Websocket Client & Server Library implementing the WebSocket protocol as specified in RFC 6455
—
Utility functions, deprecation handling, and version information for the WebSocket library.
Library version information and identification.
/**
* Current library version string
*/
const version: string; // "1.0.35"Usage Example:
const WebSocket = require('websocket');
console.log('WebSocket library version:', WebSocket.version);
// Output: WebSocket library version: 1.0.35
// Check version programmatically
const majorVersion = parseInt(WebSocket.version.split('.')[0]);
if (majorVersion >= 1) {
console.log('Using modern WebSocket library');
}Deprecation warning system for managing deprecated functionality.
/**
* Deprecation management
*/
class Deprecation {
/** Disable all deprecation warnings */
static disableWarnings: boolean;
/** Map of deprecation warning messages */
static deprecationWarningMap: object;
/**
* Show deprecation warning
* @param deprecationName - Name of deprecated feature
*/
static warn(deprecationName: string): void;
}Usage Example:
const WebSocket = require('websocket');
// Disable deprecation warnings globally
WebSocket.deprecation.disableWarnings = true;
// Or handle specific deprecation warnings
WebSocket.deprecation.warn('oldFeature');
// Check if warnings are enabled
if (!WebSocket.deprecation.disableWarnings) {
console.log('Deprecation warnings are enabled');
}Cross-platform utility functions for internal library operations.
/**
* Utility functions (mostly internal use)
*/
interface Utils {
/** No-operation function */
noop(): void;
/**
* Extend object with properties from another object
* @param dest - Destination object
* @param source - Source object
* @returns Extended object
*/
extend(dest: object, source: object): object;
/**
* Cross-version event listener count
* @param emitter - EventEmitter instance
* @param type - Event type
* @returns Number of listeners
*/
eventEmitterListenerCount(emitter: EventEmitter, type: string): number;
/**
* Cross-version buffer allocation
* @param size - Buffer size in bytes
* @returns Allocated buffer
*/
bufferAllocUnsafe(size: number): Buffer;
/**
* Cross-version buffer from string
* @param string - String to convert
* @param encoding - String encoding
* @returns Buffer from string
*/
bufferFromString(string: string, encoding?: string): Buffer;
/**
* Create buffering logger
* @param identifier - Logger identifier
* @param uniqueID - Unique logger ID
* @returns Logger function
*/
BufferingLogger(identifier: string, uniqueID: string): Function;
}Usage Example:
// These utilities are primarily for internal use
// but can be accessed if needed for advanced usage
const WebSocket = require('websocket');
// Access internal utilities (not recommended for normal use)
const utils = require('websocket/lib/utils');
// Extend objects
const config = { timeout: 5000 };
const extendedConfig = utils.extend(config, { keepalive: true });
console.log(extendedConfig); // { timeout: 5000, keepalive: true }
// Cross-platform buffer operations
const buffer = utils.bufferAllocUnsafe(100);
const stringBuffer = utils.bufferFromString('Hello World', 'utf8');
// No-op function for default callbacks
const callback = utils.noop;Browser-specific exports and compatibility handling.
/**
* Browser-specific exports
* Available when using websocket/lib/browser
*/
interface BrowserExports {
/** W3C WebSocket for browsers (may be null if native WebSocket available) */
w3cwebsocket: typeof W3CWebSocket | null;
/** Library version */
version: string;
}Usage Example:
// In browser environments
const browserWebSocket = require('websocket/lib/browser');
// Use native WebSocket if available, fallback to library implementation
const WebSocketClass = browserWebSocket.w3cwebsocket || WebSocket;
if (WebSocketClass) {
const client = new WebSocketClass('ws://localhost:8080/');
console.log('Using WebSocket implementation');
} else {
console.log('No WebSocket implementation available');
}
// Check library version in browser
console.log('Library version:', browserWebSocket.version);Custom Configuration Extension:
const WebSocket = require('websocket');
const utils = require('websocket/lib/utils');
// Create default configuration
const defaultConfig = {
maxReceivedFrameSize: 64 * 1024,
maxReceivedMessageSize: 1024 * 1024,
keepalive: true
};
// Extend with user configuration
function createServerConfig(userConfig) {
return utils.extend({}, utils.extend(defaultConfig, userConfig || {}));
}
const config = createServerConfig({
keepalive: false,
customOption: 'value'
});
const wsServer = new WebSocket.server(config);Cross-Version Compatibility:
const WebSocket = require('websocket');
const utils = require('websocket/lib/utils');
// Handle different Node.js versions
function createBuffer(size) {
// Uses appropriate buffer allocation method for the Node.js version
return utils.bufferAllocUnsafe(size);
}
function stringToBuffer(str) {
// Uses appropriate string to buffer conversion
return utils.bufferFromString(str, 'utf8');
}
// Event listener management across Node.js versions
function getListenerCount(emitter, eventType) {
return utils.eventEmitterListenerCount(emitter, eventType);
}interface DeprecationConfig {
disableWarnings: boolean;
deprecationWarningMap: {
[key: string]: string;
};
}interface BufferingLoggerFunction {
(message: string): void;
dump(): string[];
clear(): void;
}Install with Tessl CLI
npx tessl i tessl/npm-websocket