Registry / messaging / websocket-lib

websocket-lib

JSON →
library3.3.0jsnpmunverified

A lightweight WebSocket library for Node.js at version 3.3.0 (released 2023). It provides a simple API to create WebSocket servers and clients, with session-based event handling (data, close). The library supports both Node.js and browser environments. Key differentiators: minimal dependencies, straightforward stream-like API, and no external WebSocket library required (uses native 'net' and 'crypto' modules). Release cadence is sporadic, with the last update in 2023.

npm install websocket-lib
INSTALL
IMPORT
SIG · WEBSOCKET-LIB
W
websocket-lib
messagingjavascriptv3.3.0
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.

createServer
import { createServer } from 'websocket-lib'
const createServer = require('websocket-lib').createServer
Since v3, the library is ESM-only. Use named import for server creation.
connect
import { connect } from 'websocket-lib'
const ws = require('websocket-lib'); ws.connect(...)
Named import 'connect' is preferred over default import.
default import
import ws from 'websocket-lib'
const ws = require('websocket-lib')
The library exports both a default object and named exports. Default import gives access to both createServer and connect as methods.

Creates a WebSocket server on port 8080, echos back messages with 'Hello ', and a client connects, sends 'Client', and logs the server response.

import { createServer, connect } from 'websocket-lib'; // Server const server = createServer((session) => { console.log('Client connected'); session.on('data', (data) => { console.log('Received:', data); session.send('Hello ' + data); }); session.on('close', () => console.log('Session closed')); }); server.listen(8080, () => console.log('Server on :8080')); // Client const client = connect('ws://localhost:8080', (session) => { session.setEncoding('utf8'); session.send('Client'); session.on('data', (data) => console.log('Server:', data)); });
Debug
Known issues
breakingEventEmitter removal: 'connection' event removed from server.
fix
Use session callback in createServer() instead of server.on('connection', ...).
affects: >=3.0.0
breakingThe library is ESM-only since v3. CommonJS require() will throw an error.
fix
Use ESM imports (import ... from 'websocket-lib') or use dynamic import() inside CJS.
affects: >=3.0.0
gotchaSession 'data' event emits raw Buffer; must call session.setEncoding('utf8') for string.
fix
Add session.setEncoding('utf8') in the connection callback to receive strings.
affects: >=1.0.0
gotchaBrowser usage requires native WebSocket; the library only works on Node.js.
fix
Use the global WebSocket class in browsers; do not import websocket-lib client-side.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: require(...).createServer is not a function
Using CommonJS require with v3 which is ESM-only.
fix
Switch to ESM: import { createServer } from 'websocket-lib'
Error [ERR_REQUIRE_ESM]: require() of ES Module /node_modules/websocket-lib/index.js from ... not supported.
Trying to require an ESM module in CommonJS.
fix
Use import() dynamic import or convert project to ESM.
TypeError: session.send is not a function
Using event listener 'connection' instead of session callback.
fix
Use createServer((session) => { session.send(...) })
Upgrade
Version history
3.3.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
1
Resources
websocket-lib — npm install websocket-lib · libregistry