Registry / communication / simple-ng-websocket

simple-ng-websocket

JSON →
library0.3.1jsnpmunverified

Simple-ng-websocket (v0.3.1) is a lightweight, minimal Angular wrapper around the browser's native WebSocket API. It provides a straightforward injectable service with event-based messaging (open, message, close, error) and supports optional JSON serialization. This package requires Angular >=12 and offers no reconnection logic, observables, or advanced features; it's designed for simple real-time communication without the overhead of larger libraries like ngx-socket-io or rxjs-websocket. Being an early release, it has minimal community adoption and limited ongoing maintenance.

npm install simple-ng-websocket
INSTALL
IMPORT
SIG · SIMPLE-NG-WEBSOCKE
S
simple-ng-websocket
communicationjavascriptv0.3.1
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.

SimpleNgWebSocket
import { SimpleNgWebSocket } from 'simple-ng-websocket';
const SimpleNgWebSocket = require('simple-ng-websocket');
The library ships TypeScript definitions; use ES module imports in Angular projects.
CONNECT_URL
import { CONNECT_URL } from 'simple-ng-websocket';
import { ConnectUrl } from 'simple-ng-websocket';
The injection token is named CONNECT_URL (uppercase) as exported, not ConnectUrl.
LOGGER
import { LOGGER } from 'simple-ng-websocket';
Injection token for custom logger; must provide a function with signature (level: string, message: string) => void.

Shows how to provide the WebSocket service in an Angular module using CONNECT_URL and LOGGER tokens, and use it inside an injectable service with event listeners.

import { NgModule } from '@angular/core'; import { SimpleNgWebSocket, CONNECT_URL, LOGGER } from 'simple-ng-websocket'; @NgModule({ providers: [ { provide: CONNECT_URL, useValue: 'wss://echo.websocket.org' }, { provide: LOGGER, useValue: (level: string, msg: string) => console.log(`[${level}] ${msg}`) }, SimpleNgWebSocket ] }) export class AppModule {} // In a service: import { Injectable } from '@angular/core'; import { SimpleNgWebSocket } from 'simple-ng-websocket'; @Injectable() export class ChatService { constructor(private ws: SimpleNgWebSocket) { this.ws.on('message', (ev: MessageEvent) => { console.log('Received:', ev.data); }); this.ws.on('open', () => console.log('Connected')); } send(msg: string): void { this.ws.send(msg, true); // JSON.stringify by default } }
Debug
Known issues
deprecatedThe constructor's url and logger parameters are deprecated; use the injection tokens CONNECT_URL and LOGGER instead.
fix
Provide tokens in NgModule and let the constructor inject them automatically.
affects: >=0.1.0
gotchaCalling send() before connect() will automatically call connect(), which may cause unexpected multiple connections if not careful.
fix
Always ensure the WebSocket is connected before sending by listening to the 'open' event.
affects: all
gotchaThe send() method defaults to JSON.stringify the message. If you send already-stringified JSON, it will be double-encoded.
fix
Pass false as the second argument to send() when the message is already a string.
affects: all
gotchaThis library does not implement automatic reconnection. If the connection drops, you must manually reconnect.
fix
Listen to 'close' event and call connect() to re-establish the connection.
affects: all
Errors
Common errors & fixes
NullInjectorError: No provider for InjectionToken CONNECT_URL!
CONNECT_URL injection token not provided in Angular module.
fix
Add { provide: CONNECT_URL, useValue: 'wss://your-server.com/ws' } to the providers array.
TypeError: ngws.send is not a function
Attempting to use SimpleNgWebSocket.send() before injection is complete or using wrong import.
fix
Ensure SimpleNgWebSocket is injected via constructor, and import it correctly: import { SimpleNgWebSocket } from 'simple-ng-websocket';
Property 'on' does not exist on type 'SimpleNgWebSocket'
Using Angular version with strict type checking and missing event type definitions.
fix
Upgrade to the latest version or cast: (ngws as any).on(...). Alternatively, the 'on' method expects string event name and callback.
Upgrade
Version history
0.3.1latest on npm
Audit
Dependencies
@angular/corerequiredPeer dependency required for Angular dependency injection
@angular/commonrequiredPeer dependency required for Angular common utilities
Agent activity
18 hits · last 30 days
node
16
OpenAI (training)
1
Resources
simple-ng-websocket — npm install simple-ng-websocket · libregistry