Gathering detailed insights and metrics for ngx-socket-io
Gathering detailed insights and metrics for ngx-socket-io
Gathering detailed insights and metrics for ngx-socket-io
Gathering detailed insights and metrics for ngx-socket-io
npm install ngx-socket-io
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
261 Stars
161 Commits
89 Forks
18 Watching
1 Branches
24 Contributors
Updated on 27 Nov 2024
Minified
Minified + Gzipped
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
0.3%
7,415
Compared to previous day
Last week
0.8%
34,550
Compared to previous week
Last month
15%
140,068
Compared to previous month
Last year
22.6%
1,591,894
Compared to previous year
3
Socket.IO module for Angular
npm install ngx-socket-io
Important:
Make sure you're using the proper corresponding version of socket.io on the server.
Package Version | Socket-io Server Version | Angular version |
---|---|---|
v3.4.0 | v2.2.0 | |
v4.1.0 | v4.0.0 | 12.x |
v4.2.0 | v4.0.0 | 13.x |
v4.3.0 | v4.5.1 | 14.x |
v4.4.0 | v4.5.1 | 15.x |
v4.5.0 | v4.5.1 | 16.x |
v4.6.1 | v4.7.2 | 17.x |
v4.7.0 | v4.7.2 | 18.x |
v4.8.1 | v4.8.1 | 19.x |
1import { SocketIoModule, SocketIoConfig } from 'ngx-socket-io'; 2 3const config: SocketIoConfig = { url: 'http://localhost:8988', options: {} }; 4 5@NgModule({ 6 declarations: [AppComponent], 7 imports: [BrowserModule, SocketIoModule.forRoot(config)], 8 providers: [], 9 bootstrap: [AppComponent], 10}) 11export class AppModule {}
We need to configure SocketIoModule
module using the object config
of type SocketIoConfig
, this object accepts two optional properties they are the same used here io(url[, options]).
Now we pass the configuration to the static method forRoot
of SocketIoModule
In app.config.ts use the following:
1import { ApplicationConfig, importProvidersFrom } from '@angular/core'; 2import { SocketIoModule, SocketIoConfig } from 'ngx-socket-io'; 3 4const config: SocketIoConfig = { url: 'http://localhost:8988', options: {} }; 5 6export const appConfig: ApplicationConfig = { 7 providers: [importProvidersFrom(SocketIoModule.forRoot(config))], 8};
For standalone applications we do not have the AppModule where we can import the SocketIoModule
. Instead we can use the importProvidersFrom
provided by angular to provide the SocketIoModule
to our application. The usage of the socket instance is then the same as if we used a NgModule based application.
The SocketIoModule
provides now a configured Socket
service that can be injected anywhere inside the AppModule
.
1import { Injectable } from '@angular/core'; 2import { Socket } from 'ngx-socket-io'; 3import { map } from 'rxjs/operators'; 4 5@Injectable() 6export class ChatService { 7 constructor(private socket: Socket) {} 8 9 sendMessage(msg: string) { 10 this.socket.emit('message', msg); 11 } 12 getMessage() { 13 return this.socket.fromEvent('message').pipe(map(data => data.msg)); 14 } 15}
In this case we do not configure the SocketIoModule
directly using forRoot
. What we have to do is: extend the Socket
service, and call super()
with the SocketIoConfig
object type (passing url
& options
if any).
1import { Injectable, NgModule } from '@angular/core'; 2import { Socket } from 'ngx-socket-io'; 3 4@Injectable() 5export class SocketOne extends Socket { 6 constructor() { 7 super({ url: 'http://url_one:portOne', options: {} }); 8 } 9} 10 11@Injectable() 12export class SocketTwo extends Socket { 13 constructor() { 14 super({ url: 'http://url_two:portTwo', options: {} }); 15 } 16} 17 18@NgModule({ 19 declarations: [ 20 //components 21 ], 22 imports: [ 23 SocketIoModule, 24 //... 25 ], 26 providers: [SocketOne, SocketTwo], 27 bootstrap: [ 28 /** AppComponent **/ 29 ], 30}) 31export class AppModule {}
Now you can inject SocketOne
, SocketTwo
in any other services and / or components.
Most of the functionalities here you are already familiar with.
The only addition is the fromEvent
method, which returns an Observable
that you can subscribe to.
socket.of(namespace: string)
Takes an namespace. Works the same as in Socket.IO.
socket.on(eventName: string, callback: Function)
Takes an event name and callback. Works the same as in Socket.IO.
socket.removeListener(eventName: string, callback?: Function)
Takes an event name and callback. Works the same as in Socket.IO.
socket.removeAllListeners(eventName?: string)
Takes an event name. Works the same as in Socket.IO.
socket.emit(eventName:string, ...args: any[])
Sends a message to the server. Works the same as in Socket.IO.
socket.fromEvent<T>(eventName: string): Observable<T>
Takes an event name and returns an Observable that you can subscribe to.
socket.fromOneTimeEvent<T>(eventName: string): Promise<T>
Creates a Promise for a one-time event.
You should keep a reference to the Observable subscription and unsubscribe when you're done with it.
This prevents memory leaks as the event listener attached will be removed (using socket.removeListener
) ONLY and when/if you unsubscribe.
If you have multiple subscriptions to an Observable only the last unsubscription will remove the listener.
For error TS2345
you need to add this to your tsconfig.json
.
1{ 2 ... 3 "compilerOptions": { 4 ... 5 "paths": { 6 "rxjs": ["node_modules/rxjs"] 7 } 8 }, 9}
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
8 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 6
Reason
Found 4/20 approved changesets -- score normalized to 2
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More@supy-io/ngx-intercom
[![npm](https://img.shields.io/npm/v/@supy-io/ngx-intercom.svg)](https://www.npmjs.com/package/@supy-io/ngx-intercom) [![npm](https://img.shields.io/npm/dm/@supy-io/ngx-intercom.svg)](https://www.npmjs.com/@supy-io/ngx-intercom)
nuxt-socket-io
Socket.io client and server module for Nuxt. Just plug it in and GO
angular-socket-io
Bower Component for using AngularJS with [Socket.IO](http://socket.io/), based on [this](http://briantford.com/blog/angular-socket-io.html).
mock-socket
Javascript mocking library for websockets and socket.io