Registry / messaging / socket-sorcerer

socket-sorcerer

JSON →
library7.0.0jsnpmunverified

A lightweight WebSocket framework for Node.js built on top of the 'ws' package. Version 7.0.0 provides server and client modules with built-in reconnection, pinging, authentication, channel-based messaging, and event handling. It simplifies real-time communication with minimal boilerplate compared to raw 'ws' or Socket.IO. The library ships TypeScript definitions and supports ESM imports. Release cadence is not specified; maintained by Paul Lazunko on GitHub.

npm install socket-sorcerer
INSTALL
IMPORT
SIG · SOCKET-SORCERER
S
socket-sorcerer
messagingjavascriptv7.0.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.

WebSocketServer
import { WebSocketServer } from 'socket-sorcerer/server';
import WebSocketServer from 'socket-sorcerer/server';
Named export from '/server' subpath; default import will not work.
WebSocketClient
import { WebSocketClient } from 'socket-sorcerer/client';
const WebSocketClient = require('socket-sorcerer/client');
Named export from '/client' subpath; CommonJS require would import a CommonJS module if available, but the package is ESM-first.
Manager
import type { Manager } from 'socket-sorcerer';
import { Manager } from 'socket-sorcerer';
Manager is returned by wss.getManager() and used as a type; not a direct export.
ServerOptions
import type { ServerOptions } from 'socket-sorcerer/server';
TypeScript type available from '/server' subpath.
ClientOptions
import type { ClientOptions } from 'socket-sorcerer/client';
TypeScript type available from '/client' subpath.

Creates a WebSocket server with authentication and ping, then a client that connects with reconnection and sends messages.

import { WebSocketServer } from 'socket-sorcerer/server'; import { WebSocketClient } from 'socket-sorcerer/client'; import { createServer } from 'http'; const server = createServer().listen(8088); const wss = new WebSocketServer({ serverOptions: { server }, pingInterval: 5000, pingTimeout: 15000, authenticate: { eventName: 'auth', async eventHandler(token) { const user = { _id: '5e1c62a969a07513e8f99a73' }; return user._id; }, authTimeout: 3000 }, events: { connect(connectionId, userId) { wss.getManager().join({ channel: 'default', user: userId }); }, flud(data, connectionId, userId) { console.log('Received:', data); }, disconnect(connectionId, userId) {} } }); // Client (browser) const ws = new WebSocketClient({ serverUrl: 'ws://localhost:8088', token: 'myToken', doReconnectOnClose: true, reconnectInterval: 5000, authEventName: 'auth', events: { update: (data) => { console.log('Update:', data); } } }); // Send from client ws.emit({ channel: 'flud', event: 'message', data: { text: 'Hello' } });
Debug
Known issues
breakingIn version 7.0.0, the import paths changed: server and client are now subpath exports (e.g., 'socket-sorcerer/server'). Previous versions used a single export 'socket-sorcerer'.
fix
Update imports to use subpath exports: import { WebSocketServer } from 'socket-sorcerer/server'; and import { WebSocketClient } from 'socket-sorcerer/client';
affects: >=6.0.0 <7.0.0
breakingThe 'authenticate.eventHandler' must return a user ID (string) or a Promise of user ID. Returning an object or number will cause unexpected behavior.
fix
Ensure the eventHandler returns a string user ID.
affects: >=7.0.0
deprecatedThe 'events.connect' and 'events.disconnect' callbacks receive (connectionId, userId, token, userIp) in version 7.0.0, but earlier versions passed only userId. Signature changed.
fix
Update event handlers to accept the new parameter order.
affects: >=7.0.0
gotchaThe client-side 'authEventName' must match the server's 'authenticate.eventName'. Default is 'auth' but can be customized.
fix
Verify client and server use the same auth event name.
affects: >=7.0.0
gotchaThe token sent by the client can be a string or an object with a 'token' property. Other formats may fail authentication.
fix
Ensure the client token is either a string or an object containing a 'token' property.
affects: >=7.0.0
Errors
Common errors & fixes
TypeError: wss.getManager is not a function
Calling getManager before the WebSocketServer is fully initialized or authentication completed (if authentication is enabled).
fix
Call wss.getManager() only inside event handlers like 'connect' or after the server has been instantiated (authentication must succeed).
Error: Invalid import path: 'socket-sorcerer' (use '/server' or '/client' subpath instead)
Importing from 'socket-sorcerer' without subpath is not allowed in version 7.
fix
Use correct subpath: import { WebSocketServer } from 'socket-sorcerer/server'; or import { WebSocketClient } from 'socket-sorcerer/client';
TypeError: Cannot read properties of undefined (reading '_id')
The authenticate eventHandler did not return a value or returned undefined.
fix
Make sure the eventHandler returns a user ID (string) or a Promise resolving to a string.
WebSocket connection to 'ws://...' failed: Authentication timeout
The client did not send the auth event within the server's 'authTimeout' period.
fix
Increase the server's 'authTimeout' or ensure the client sends the auth event promptly.
Upgrade
Version history
7.0.0latest on npm
Audit
Dependencies
wsrequiredUnderlying WebSocket implementation; provides low-level server and client.
Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
1
Resources
socket-sorcerer — npm install socket-sorcerer · libregistry