Registry /
messaging / angular2-websocket-service
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();
}
}
Errors
Common errors & fixes
Cannot find module 'angular2-websocket-service'
Typo in package name or not installed
fixRun 'npm install angular2-websocket-service'
Property 'fromEvent' does not exist on type 'typeof Observable'
RxJS v6 has deprecated Observable.fromEvent; library uses old import
fixInstall rxjs-compat: 'npm install rxjs-compat' or migrate to newer library
NullInjectorError: No provider for WebSocketService
WebSocketService not listed in module providers
fixAdd WebSocketService to providers array in your NgModule
TypeError: this.inputStream.next is not a function
QueueingSubject not installed or not passed to connect()
fixInstall queueing-subject: 'npm install queueing-subject' and pass instance as second argument
Audit
Dependencies
queueing-subjectoptionalRecommended for message queuing when socket disconnects
rxjsrequiredPeer dependency; uses RxJS v5 Observable and Subject