Installations
npm install ngx-socket-io
Developer Guide
Typescript
No
Module System
CommonJS, ESM
Node Version
18.19.1
NPM Version
10.2.4
Score
92
Supply Chain
95.9
Quality
89
Maintenance
100
Vulnerability
98.9
License
Releases
Contributors
Languages
TypeScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
rodgc
Download Statistics
Total Downloads
5,931,908
Last Day
1,168
Last Week
40,450
Last Month
160,025
Last Year
1,672,198
GitHub Statistics
MIT License
264 Stars
170 Commits
91 Forks
18 Watchers
1 Branches
25 Contributors
Updated on Feb 09, 2025
Bundle Size
56.07 kB
Minified
17.14 kB
Minified + Gzipped
Package Meta Information
Latest Version
4.8.4
Package Id
ngx-socket-io@4.8.4
Unpacked Size
36.36 kB
Size
9.21 kB
File Count
9
NPM Version
10.2.4
Node Version
18.19.1
Published on
Feb 09, 2025
Total Downloads
Cumulative downloads
Total Downloads
5,931,908
Last Day
-7.6%
1,168
Compared to previous day
Last Week
4.4%
40,450
Compared to previous week
Last Month
33.9%
160,025
Compared to previous month
Last Year
24.7%
1,672,198
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
3
ngx-socket-io
Socket.IO module for Angular
Install
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 |
How to use
Import and configure SocketIoModule for NgModule based applications
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
Import and configure SocketIoModule for standalone based applications
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.
Using your socket Instance
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}
Using multiple sockets with different end points
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.
API
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 a namespace and returns an instance based on the current config and the given namespace, that is added to the end of the current url. See Namespaces - Client Initialization. Instances are reused based on the namespace.
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.
Know Issue
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}
Related projects
- bougarfaoui/ng-socket-io - Socket.IO module for Angular
LICENSE
MIT
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
13 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/close-inactive-issues.yml:1
- Info: no jobLevel write permissions found
Reason
2 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67mh-4wv8-2f99
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
Reason
Found 5/25 approved changesets -- score normalized to 2
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/close-inactive-issues.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/rodgc/ngx-socket-io/close-inactive-issues.yml/master?enable=pin
- Info: 0 out of 1 GitHub-owned GitHubAction dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 10 are checked with a SAST tool
Score
5.2
/10
Last Scanned on 2025-02-10
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 MoreOther packages similar to ngx-socket-io
@rxap/socket-io
Provides Angular providers for integrating Socket.IO client functionality. It simplifies the configuration and injection of a Socket.IO client instance using tokens and dynamic configuration options. This package allows you to easily connect to a Socket.I
ngx-person-tracker-ui-socket-io
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 11.2.14.
ngx-httpcommons-es-socket-io
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 11.0.9.
@helveg/ngx-socket-io
Fork of `ngx-socket-io`. Socket.IO module for Angular