Registry / messaging / sockjs-broker-client

sockjs-broker-client

JSON →
library1.2.12jsnpmunverified

A WebSocket client library for connecting to sockjs-broker servers. Version 1.2.12 provides a client base for establishing WebSocket connections via the SockJS protocol, supporting reconnection, event handling, and message exchange. Released with low update frequency, it is intended for use with the sockjs-broker server-side component. Key differentiators include lightweight implementation and compatibility with SockJS fallback transports for browser environments, but limited documentation and ecosystem support compared to alternatives like native WebSocket or Socket.IO.

npm install sockjs-broker-client
INSTALL
IMPORT
SIG · SOCKJS-BROKER-CLIE
S
sockjs-broker-client
messagingjavascriptv1.2.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.

SockJSBrokerClient
import { SockJSBrokerClient } from 'sockjs-broker-client'
const SockJSBrokerClient = require('sockjs-broker-client').SockJSBrokerClient
ESM named export; CJS requires destructuring. Available since v1.0.0.
default
import SockJSBrokerClient from 'sockjs-broker-client'
import { default } from 'sockjs-broker-client'
Default export available; no need for named default. Works with both CJS and ESM.
EventEmitter
import { EventEmitter } from 'sockjs-broker-client'
import EventEmitter from 'sockjs-broker-client'
Named export for EventEmitter class; not default. Introduced in v1.2.0.
SockJSBrokerClient (type)
import type { SockJSBrokerClient } from 'sockjs-broker-client'
TypeScript users should use type import for type-only usage. Works in TypeScript 3.8+.

Creates a SockJSBrokerClient connecting to a broker, subscribes to a channel, and logs messages.

import { SockJSBrokerClient } from 'sockjs-broker-client'; const client = new SockJSBrokerClient({ url: 'http://localhost:9999/broker', token: process.env.BROKER_TOKEN ?? '', reconnect: true, transports: ['websocket', 'xhr-streaming', 'xhr-polling'] }); client.on('open', () => { console.log('Connected'); client.send({ type: 'subscribe', channel: 'test' }); }); client.on('message', (data) => { console.log('Received:', data); }); client.on('error', (err) => { console.error('Error:', err.message); }); client.connect(); setTimeout(() => { client.close(); }, 5000);
Debug
Known issues
breakingSockJSBrokerClient constructor options changed in v1.2.0: 'token' replaces 'authToken'.
fix
Use 'token' instead of 'authToken' in options object.
affects: >=1.1.0 <1.2.0
deprecatedUsing 'require('sockjs-broker-client')' is deprecated in favor of ESM imports.
fix
Use import { SockJSBrokerClient } from 'sockjs-broker-client'.
affects: >=1.2.0
gotchaClient throws 'WebSocket is not defined' in Node.js versions <18 if global WebSocket not polyfilled.
fix
Install 'ws' and assign global.WebSocket = require('ws'); or use Node >=18 with native WebSocket.
affects: >=1.0.0
gotchaDefault transport order may silently fallback to older transports; ensure correct server configuration.
fix
Set transports option to ['websocket'] to force WebSocket only if server supports it.
affects: >=1.0.0
gotchaSockJSBrokerClient uses 'sockjs-client' which has known CORS issues in some browsers without proper server headers.
fix
Ensure server sends Access-Control-Allow-Origin and Access-Control-Allow-Credentials headers.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: SockJSBrokerClient is not a constructor
CommonJS require without destructuring the named export.
fix
Use const { SockJSBrokerClient } = require('sockjs-broker-client');
Error: connect ECONNREFUSED 127.0.0.1:9999
No broker server running at specified URL.
fix
Start sockjs-broker server on the expected host:port before client connection.
Unhandled 'error' event: ReferenceError: WebSocket is not defined
WebSocket not available in Node.js environment (pre-v18 or missing polyfill).
fix
Install 'ws' and set global.WebSocket = require('ws');
TypeError: client.send is not a function
Calling send before client is connected or after close.
fix
Ensure client is connected via 'open' event before calling send.
Upgrade
Version history
1.2.12latest on npm
Audit
Dependencies
sockjs-clientrequiredProvides SockJS WebSocket-like client with fallback transports
Agent activity
14 hits · last 30 days
node
12
Amazon
1
OpenAI (training)
1
Resources
sockjs-broker-client — npm install sockjs-broker-client · libregistry