Cryo-Server
raw JSON → 2.9.4 verified Sat May 09 auth: no javascript
Cryo-Server is a lightweight WebSocket server framework for Node.js that handles client authentication, session lifecycle management, and data framing. Current stable version is 2.9.4. Part of the Cryo ecosystem with client implementations for TypeScript/JavaScript (Node.js and browsers) and C# (.NET). Key differentiators: built-in backpressure support with configurable drop policies, SSL/TLS support, and an extension interface for building RPC or other protocols on top. Released on npm, with infrequent updates.
Common errors
error ERR_REQUIRE_ESM: require() of ES Module /path/to/node_modules/cryo-server/dist/index.js from /path/to/app.js not supported. ↓
cause Using CommonJS require() with an ESM-only package.
fix
Switch to import syntax or set "type": "module" in your package.json.
error TypeError: cryo is not a function ↓
cause Importing the default export instead of the named export cryo.
fix
Use import { cryo } from 'cryo-server' instead of import cryo from 'cryo-server'.
error TypeError: Cannot read properties of undefined (reading 'validate') ↓
cause Passing an empty object instead of an ITokenValidator as the first argument.
fix
Provide a valid token validator object with a validate method.
Warnings
breaking In v2.0.0, the package switched to ESM-only. Using require() throws an error. ↓
fix Use import { ... } from 'cryo-server' and ensure package.json includes "type": "module".
breaking In v2.0.0, the cryo function signature changed: the port option moved from a separate parameter to inside the options object. ↓
fix Pass port inside the options object: cryo(validator, { port: 8080 }) instead of cryo(port, validator).
gotcha If no backpressure configuration is provided, default values are used which may not suit high-load scenarios. ↓
fix Explicitly configure backpressure options based on your expected load.
gotcha The validate function in ITokenValidator must return a boolean. Returning undefined or null will be treated as false. ↓
fix Always return true or false explicitly.
Install
npm install cryo-server yarn add cryo-server pnpm add cryo-server Imports
- cryo wrong
const cryo = require('cryo-server')correctimport { cryo } from 'cryo-server' - ITokenValidator wrong
import { ITokenValidator } from 'cryo-server'correctimport type { ITokenValidator } from 'cryo-server' - ICryoWebsocketServerOptions wrong
import { ICryoWebsocketServerOptions } from 'cryo-server'correctimport type { ICryoWebsocketServerOptions } from 'cryo-server'
Quickstart
import { cryo } from 'cryo-server';
const tokenValidator = {
validate: async (token: string): Promise<boolean> => {
// Simple example: accept tokens that are not empty
return token.length > 0;
}
};
const options = {
port: 8080,
keepAliveIntervalMs: 15000,
backpressure: {
highWaterMark: 16 * 1024 * 1024,
lowWaterMark: 1024 * 1024,
maxQueuedBytes: 8 * 1024 * 1024,
maxQueueCount: 1024,
dropPolicy: "drop-oldest"
}
};
const server = cryo(tokenValidator, options);
server.on('session:open', (session) => {
console.log('Session opened:', session.id);
session.send('Welcome!');
});
server.on('session:message', (session, data) => {
console.log('Received:', data);
session.send('Echo: ' + data);
});
server.on('session:close', (session) => {
console.log('Session closed:', session.id);
});