Registry / messaging / browser-websocket

browser-websocket

JSON →
library2.2.0jsnpmunverified

browser-websocket is a small wrapper around the native WebSocket API designed to simplify websocket usage in browser and Node.js environments. Version 2.2.0 is the latest stable release, but the package appears dormant with no recent updates. It provides an EventEmitter-like interface (on/emit) for handling WebSocket events, making it similar to socket.io but without the complex protocols. The package aims to offer a drop-in solution for browser websocket communication with a simple API. However, it lacks TypeScript types and has known event-handling pitfalls.

npm install browser-websocket
INSTALL
IMPORT
SIG · BROWSER-WEBSOCKET
B
browser-websocket
messagingjavascriptv2.2.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.

BrowserWebSocket
const BrowserWebSocket = require('browser-websocket')
import BrowserWebSocket from 'browser-websocket'
Package is CommonJS only; there is no ESM default export.
BrowserWebSocket
<script src='browser-websocket.min.js'></script>
In browser, the global `BrowserWebSocket` is exposed via the minified script.
ws.on('open', ...)
ws.on('open', function() { ... })
ws.addEventListener('open', ...)
The package uses its own event system (on/emit), not native addEventListener. Use 'on' to listen for events.

Create a websocket connection to ws://localhost:8000, emit a message on open, log incoming messages and close event.

const BrowserWebSocket = require('browser-websocket'); const ws = new BrowserWebSocket('ws://localhost:8000'); ws.on('open', function() { ws.emit('hello world'); }); ws.on('message', function(e) { console.log(e.data); }); ws.on('close', function() { console.log('closed'); });
Debug
Known issues
gotchaThe 'emit' method on the WebSocket instance sends messages, but users may expect 'ws.send' as in native WebSocket API.
fix
Use ws.emit(data) to send messages; there is no send method.
affects: >=0.0.0
gotchaThe 'message' event exposes the data via e.data, but users might expect e directly as a string.
fix
Access e.data for the message content.
affects: >=0.0.0
deprecatedThe package is no longer actively maintained; consider switching to native WebSocket or socket.io for newer projects.
fix
Migrate to the standard WebSocket API (new WebSocket(url)) or use a maintained library.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: BrowserWebSocket is not a constructor
Importing the package incorrectly in an ESM environment; this package only supports CommonJS.
fix
Use require('browser-websocket') instead of import; if using TypeScript, add // @ts-ignore or use a dynamic import.
ws.send is not a function
Trying to call send() as in the native WebSocket API, but the package uses emit() for sending.
fix
Replace ws.send(data) with ws.emit(data).
Upgrade
Version history
2.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
33 hits · last 30 days
node
28
OpenAI (training)
1
Resources
browser-websocket — npm install browser-websocket · libregistry