Registry / testing / fake-websocket

fake-websocket

JSON →
library1.0.9jsnpmunverified

Package for simulating a WebSocket server and client entirely in the browser, useful for end-to-end testing without a real server. Current stable version is 1.0.9, updated on 2020-04-16, with low release cadence. Key differentiators: replaces native WebSocket for local simulation, enables testing and reverse engineering scenarios like WeChat DevTools communication. No further versions published recently.

npm install fake-websocket
INSTALL
IMPORT
SIG · FAKE-WEBSOCKET
F
fake-websocket
testingjavascriptv1.0.9
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 'fake-websocket'
const WebSocketServer = require('fake-websocket').WebSocketServer
Named export; CommonJS require also works but use destructuring.
WebSocket
import { WebSocket } from 'fake-websocket'
import WebSocket from 'fake-websocket'
It is a named export, not default. CommonJS: const { WebSocket } = require('fake-websocket')
FakeWebSocket
import * as FakeWebSocket from 'fake-websocket'
import FakeWebSocket from 'fake-websocket'
Namespace import includes both WebSocketServer and WebSocket. The README uses namespace style.

Demonstrates creating a fake WebSocket server and connecting a fake WebSocket client in the browser, exchanging messages.

import { WebSocketServer, WebSocket } from 'fake-websocket'; // Create server const server = new WebSocketServer('ws://127.0.0.1:8080'); server.on('connection', (ws) => { ws.on('message', (e) => { console.log('Server received:', e.data); }); ws.send({ hello: 'from server' }); }); // Create client const ws = new WebSocket('ws://127.0.0.1:8080'); ws.onopen = () => { ws.send('Hello'); }; ws.onmessage = (e) => { console.log('Client received:', e.data); };
Debug
Known issues
gotchaThe package replaces the global WebSocket object if used without import. Use import carefully to avoid side effects.
fix
Always use named imports or namespace imports to avoid accidentally overriding native WebSocket.
affects: <=1.0.9
deprecatedPackage has not been updated since 2020; consider using alternatives like mock-socket for active maintenance.
fix
Migrate to mock-socket or ws (Node.js) for ongoing support.
affects: >=1.0.0
gotchaWebSocketServer is a class, not a function; forgetting 'new' may cause silent errors.
fix
Always instantiate with 'new'.
affects: >=1.0.0
gotchaThe WebSocket constructor does not support protocols array; second argument is ignored.
fix
Use mock-socket if protocol support is needed.
affects: <=1.0.9
Errors
Common errors & fixes
Uncaught ReferenceError: WebSocket is not defined
Importing from fake-websocket incorrectly (e.g., default import) or not importing at all.
fix
Use: import { WebSocket } from 'fake-websocket'
Cannot read property 'send' of undefined
WebSocketServer connection callback parameter (ws) might be undefined if event is not properly emitted.
fix
Check server URL matches client URL exactly, including protocol and port.
Failed to construct 'WebSocket': The URL 'ws://127.0.0.1:8080' is invalid.
Browser environment might block ws:// URLs in some contexts; fake-websocket works only in browser and expects valid URL format.
fix
Ensure the URL is a valid WebSocket URL; fake-websocket does not connect to real servers, so any valid URL works.
Module not found: Error: Can't resolve 'fake-websocket'
Missing installation or typo in import path.
fix
Run: npm install fake-websocket
Upgrade
Version history
1.0.9latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
fake-websocket — npm install fake-websocket · libregistry