Installations
npm install @helveg/ngx-socket-io
Developer Guide
Typescript
No
Module System
CommonJS, ESM
Node Version
16.13.2
NPM Version
8.3.1
Score
65.2
Supply Chain
93.1
Quality
74.7
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
556
Last Day
2
Last Week
6
Last Month
26
Last Year
114
GitHub Statistics
MIT License
264 Stars
170 Commits
91 Forks
18 Watchers
1 Branches
25 Contributors
Updated on Feb 09, 2025
Bundle Size
54.53 kB
Minified
16.82 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.1.1
Package Id
@helveg/ngx-socket-io@1.1.1
Unpacked Size
55.42 kB
Size
14.23 kB
File Count
17
NPM Version
8.3.1
Node Version
16.13.2
Total Downloads
Cumulative downloads
Total Downloads
556
Last Day
0%
2
Compared to previous day
Last Week
-14.3%
6
Compared to previous week
Last Month
100%
26
Compared to previous month
Last Year
-30.9%
114
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 |
---|---|
v3.4.0 | v2.2.0 |
v4.1.0 | v4.0.0 |
How to use
Import and configure SocketIoModule
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
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 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.fromEventOnce<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

No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
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
1 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
Reason
7 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 6
Reason
Found 6/22 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
project is not fuzzed
Details
- Warn: no fuzzer integrations found
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
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 14 are checked with a SAST tool
Score
4.9
/10
Last Scanned on 2025-02-03
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