Registry / messaging / angular2-websocket-service

angular2-websocket-service

JSON →
library0.5.3jsnpmunverified

Provides an injectable Angular service factory for creating RxJS stream-based WebSocket connections. Version 0.5.3 is the latest stable release. The service emits Observable streams from a single WebSocket connection, supporting automatic reconnection via a QueueingSubject pattern. Compared to raw WebSocket or other libraries, this integrates directly with Angular DI and RxJS, making it suitable for reactive Angular applications. However, the library is no longer actively maintained and relies on older RxJS v5 APIs (Observable.fromEvent, Subject) that are incompatible with RxJS v6+ without a compatibility layer.

npm install angular2-websocket-service
INSTALL
IMPORT
SIG · ANGULAR2-WEBSOCKET
A
angular2-websocket-service
messagingjavascriptv0.5.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

WebSocketService
import { WebSocketService } from 'angular2-websocket-service'
const WebSocketService = require('angular2-websocket-service')
Library ships TypeScript definitions, use ESM import.
default import
import { WebSocketService } from 'angular2-websocket-service'
import WebSocketService from 'angular2-websocket-service'
No default export; must use named import.
type import
import type { WebSocketService } from 'angular2-websocket-service'
For TypeScript type-only imports, use 'import type'.

Setup Angular module with WebSocketService provider, create a custom service wrapping the WebSocket with QueueingSubject, and use it in a component to send/receive messages.

// app.module.ts import { NgModule } from '@angular/core'; import { WebSocketService } from 'angular2-websocket-service'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; @NgModule({ imports: [BrowserModule], declarations: [AppComponent], providers: [WebSocketService], bootstrap: [AppComponent] }) export class AppModule { } // server-socket.service.ts import { Injectable } from '@angular/core'; import { QueueingSubject } from 'queueing-subject'; import { Observable } from 'rxjs/Observable'; import { WebSocketService } from 'angular2-websocket-service'; @Injectable() export class ServerSocket { private inputStream: QueueingSubject<any>; public outputStream: Observable<any>; constructor(private socketFactory: WebSocketService) {} public connect() { if (this.outputStream) return this.outputStream; this.outputStream = this.socketFactory.connect( 'ws://127.0.0.1:4201/ws', this.inputStream = new QueueingSubject<any>() ).share(); return this.outputStream; } public send(message: any): void { this.inputStream.next(message); } } // component.ts import { Component, OnDestroy } from '@angular/core'; import { Subscription } from 'rxjs/Subscription'; import { ServerSocket } from './server-socket.service'; @Component({ selector: 'socket-user', template: '<p>WebSocket demo</p>' }) export class SocketUserComponent implements OnDestroy { private sub: Subscription; constructor(private socket: ServerSocket) { this.sub = this.socket.connect().subscribe(msg => console.log(msg)); this.socket.send({ type: 'hello' }); } ngOnDestroy() { this.sub.unsubscribe(); } }
Debug
Known issues
deprecatedLibrary uses RxJS v5 observable chain (Observable.fromEvent) and is not compatible with RxJS v6+ without rxjs-compat
fix
Use rxjs-compat or migrate to a maintained library like 'rxjs-websocket' (RxJS built-in) or 'ngx-socket-io'
affects: >=0.5.3
gotchaThe WebSocketService.connect() method returns an Observable that does not complete; you must manage subscription lifecycle to avoid memory leaks
fix
Always store the subscription and unsubscribe in ngOnDestroy.
affects: >=0.1.0
breakingIn version 0.3.0, the import path changed from 'angular2-websocket-service/websocket-service' to 'angular2-websocket-service'
fix
Update import to 'angular2-websocket-service'
affects: <0.3.0
gotchaUsing .share() is essential for reusing the same WebSocket connection; without it each subscriber creates a new WebSocket
fix
Call .share() on the Observable returned by connect()
affects: >=0.1.0
deprecatedThe library uses deprecated Angular form of @Injectable() without providedIn; must be added to module providers
fix
Add WebSocketService to providers array in your NgModule
affects: >=0.5.0
Errors
Common errors & fixes
Cannot find module 'angular2-websocket-service'
Typo in package name or not installed
fix
Run 'npm install angular2-websocket-service'
Property 'fromEvent' does not exist on type 'typeof Observable'
RxJS v6 has deprecated Observable.fromEvent; library uses old import
fix
Install rxjs-compat: 'npm install rxjs-compat' or migrate to newer library
NullInjectorError: No provider for WebSocketService
WebSocketService not listed in module providers
fix
Add WebSocketService to providers array in your NgModule
TypeError: this.inputStream.next is not a function
QueueingSubject not installed or not passed to connect()
fix
Install queueing-subject: 'npm install queueing-subject' and pass instance as second argument
Upgrade
Version history
0.5.3latest on npm
Audit
Dependencies
queueing-subjectoptionalRecommended for message queuing when socket disconnects
rxjsrequiredPeer dependency; uses RxJS v5 Observable and Subject
Agent activity
29 hits · last 30 days
node
22
OpenAI (training)
2
Resources
angular2-websocket-service — npm install angular2-websocket-service · libregistry