Faye-WebSocket
Faye-WebSocket is a standards-compliant WebSocket server and client library designed for Node.js environments. Extracted from the larger Faye project, it provides a low-level, event-driven API for handling WebSocket connections (RFC 6455) and EventSource streams. Unlike a full WebSocket server, it integrates with existing Node.js HTTP servers by listening for 'upgrade' events. The latest stable version, 0.11.4, was published in May 2021. The project appears to be largely unmaintained and is considered inactive, with no new versions or significant development in the past five years. Its primary differentiation lies in providing a direct implementation of the WebSocket API without significant higher-level abstractions, allowing developers fine-grained control over the protocol.
Common errors
-
Not able to load the library in ESM context using named import.
cause Attempting to use ES module `import { WebSocket } from 'faye-websocket';` syntax in an ESM context.fixUse CommonJS `require` syntax: `const WebSocket = require('faye-websocket');`. The library does not provide native ESM exports. -
Error during WebSocket handshake: 'Connection' header value is not 'Upgrade'
cause The HTTP server or an intermediary proxy (like Nginx or Apache) is not correctly forwarding the 'Upgrade' header, or the server itself isn't set up to handle WebSocket upgrade requests.fixEnsure your HTTP server is configured to listen for and handle the `upgrade` event (e.g., `http.createServer().on('upgrade', ...)` in Node.js). If using a proxy, verify that it's configured to correctly proxy WebSocket upgrade requests, including the `Connection: Upgrade` and `Upgrade: websocket` headers. -
TypeError: WebSocket.Client is not a constructor (or similar when trying to instantiate client)
cause Incorrectly importing the `Client` constructor, often trying a named import for a submodule that is actually a property of the main export.fixThe `Client` class is a static property of the main `WebSocket` object. Import the main `WebSocket` object first, then access `WebSocket.Client`: `const WebSocket = require('faye-websocket'); const client = new WebSocket.Client(...);`.
Warnings
- breaking The license for `faye-websocket` changed from MIT to Apache 2.0 in version 0.11.3. Users should be aware of the different licensing terms, particularly if integrating into projects with specific license requirements.
- gotcha When using `wss://` (secure WebSockets), `faye-websocket` client (since 0.11.0) performs server certificate verification by default. This can cause connection failures if the server certificate is invalid, self-signed, or not trusted by the client's system root certificates.
- gotcha Detecting client-side dropped WebSocket connections can be unreliable. Browsers might not immediately trigger `onclose` events, leading to a 'half-open' connection state where the server perceives the client as active.
- breaking The library is essentially unmaintained since its last release in May 2021. It does not support native ESM imports directly, which may cause compatibility issues in modern Node.js projects that primarily use `import`/`export` syntax.
Install
-
npm install faye-websocket -
yarn add faye-websocket -
pnpm add faye-websocket
Imports
- WebSocket
import WebSocket from 'faye-websocket';
const WebSocket = require('faye-websocket'); - WebSocket.Client
import { Client } from 'faye-websocket';const WebSocket = require('faye-websocket'); const client = new WebSocket.Client('ws://example.com'); - WebSocket.isWebSocket
import { isWebSocket } from 'faye-websocket';const WebSocket = require('faye-websocket'); if (WebSocket.isWebSocket(request)) { /* ... */ }
Quickstart
const WebSocket = require('faye-websocket');
const http = require('http');
// --- WebSocket Server ---
const server = http.createServer();
server.on('upgrade', function(request, socket, body) {
if (WebSocket.isWebSocket(request)) {
const ws = new WebSocket(request, socket, body);
ws.on('message', function(event) {
console.log('Server received:', event.data);
ws.send(`Echo: ${event.data}`);
});
ws.on('close', function(event) {
console.log('Server closed:', event.code, event.reason);
ws = null;
});
ws.on('error', function(event) {
console.error('Server error:', event.message);
});
}
});
server.listen(8000, () => {
console.log('WebSocket server listening on port 8000');
});
// --- WebSocket Client (connects to the server above) ---
const clientWs = new WebSocket.Client('ws://localhost:8000/');
clientWs.on('open', function(event) {
console.log('Client connected');
clientWs.send('Hello from client!');
});
clientWs.on('message', function(event) {
console.log('Client received:', event.data);
if (event.data.includes('Echo')) {
clientWs.close(1000, 'Finished');
}
});
clientWs.on('close', function(event) {
console.log('Client closed:', event.code, event.reason);
});
clientWs.on('error', function(event) {
console.error('Client error:', event.message);
});