7.0.2 • Published 2 years ago
@d-fischer/isomorphic-ws v7.0.2
isomorphic-ws
Isomorphic implementation of WebSocket.
It uses:
- ws on Node
- global.WebSocket in browsers
Limitations
Before using this module you should know that
ws
is not perfectly API compatible with
WebSocket,
you should always test your code against both Node and browsers.
Some major differences:
- no
Serverimplementation in browsers - no support for the constructor
optionsargument in browsers
Usage
You need to install both this package and ws:
> npm i @d-fischer/isomorphic-ws wsThen just require this package:
const WebSocket = require('@d-fischer/isomorphic-ws')
const ws = new WebSocket('wss://echo.websocket.org/');
ws.onopen = function open() {
console.log('connected');
ws.send(Date.now());
};
ws.onclose = function close() {
console.log('disconnected');
};
ws.onmessage = function incoming(data) {
console.log(`Roundtrip time: ${Date.now() - data.data} ms`);
setTimeout(function timeout() {
ws.send(Date.now());
}, 500);
};