Registry / messaging / base-websocket

base-websocket

JSON →
library1.0.12jsnpmunverified

A lightweight, TypeScript-ready WebSocket module providing essential lifecycle event handlers (onOpen, onResponse, onError, onClose) and core functions (initWebSocket, send, closeWS). Version 1.0.12 offers minimal abstraction over the native WebSocket API, with no external dependencies. Suitable for projects needing a simple wrapper with TypeScript support. Release cadence is low; primarily maintenance.

npm install base-websocket
INSTALL
IMPORT
SIG · BASE-WEBSOCKET
B
base-websocket
messagingjavascriptv1.0.12
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.

initWebSocket
import { initWebSocket } from 'base-websocket'
import initWebSocket from 'base-websocket'
Named export only; no default export.
send
import { send } from 'base-websocket'
Named export; requires an active connection established via initWebSocket.
closeWS
import { closeWS } from 'base-websocket'
const { closeWS } = require('base-websocket')
ESM-only; CommonJS require() not supported in this version.

Initializes a WebSocket connection to an echo server, sends a message, logs responses, and closes after 5 seconds.

import { initWebSocket, send, closeWS } from 'base-websocket'; const ws = initWebSocket({ url: 'wss://echo.websocket.org', onOpen: () => { console.log('Connected'); send('Hello Server'); }, onResponse: (data) => { console.log('Response:', data); }, onError: (err) => { console.error('Error:', err); }, onClose: () => { console.log('Disconnected'); } }); // Close after 5 seconds setTimeout(() => closeWS(), 5000);
Debug
Known issues
gotchainitWebSocket does not return a WebSocket instance; connection management is handled internally.
fix
Use the provided send() and closeWS() functions instead of manipulating the WebSocket directly.
affects: >=1.0.0
gotchaThe package does not support automatic reconnection. Network interruptions will not be handled gracefully.
fix
Implement custom reconnection logic in the onClose or onError callbacks.
affects: >=1.0.0
gotchaThe onResponse callback receives JSON objects directly – do not call JSON.parse on the data.
fix
Assume data is already parsed; handle errors if server sends invalid JSON.
affects: >=1.0.0
deprecatedNo deprecation warnings at current version.
fix
N/A
affects: none
Errors
Common errors & fixes
Cannot read properties of undefined (reading 'send')
send() called before initWebSocket completes the connection.
fix
Call send() only inside onOpen callback to ensure the connection is established.
TypeError: closeWS is not a function
Incorrect import: using default import (import closeWS from 'base-websocket') instead of named import.
fix
Use import { closeWS } from 'base-websocket'.
ReferenceError: WebSocket is not defined
The library uses the global WebSocket constructor, which is not available in Node.js without a polyfill.
fix
Install and import a WebSocket polyfill like 'ws' before using base-websocket in Node.js.
Upgrade
Version history
1.0.12latest on npm
Audit
Dependencies
wsoptionalRequired for WebSocket support in Node.js environments; not needed in browsers.
Agent activity
30 hits · last 30 days
node
28
OpenAI (training)
1
Resources
base-websocket — npm install base-websocket · libregistry