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' } });
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).
fixCall 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.
fixUse 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.
fixMake 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.
fixIncrease the server's 'authTimeout' or ensure the client sends the auth event promptly.
Audit
Dependencies
wsrequiredUnderlying WebSocket implementation; provides low-level server and client.