@monsterbitar/isomorphic-ws
TypeScript icon, indicating that this package has built-in type declarations

5.3.1 • Public • Published

isomorphic-ws

Isomorphic implementation of WebSocket.

NOTE: This is a fork that simply exports the package as an ESM support version in addition to the existing packages.

It uses:

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 Server implementation in browsers
  • no support for the constructor options argument in browsers

Usage

You need to install both this package and ws:

> npm i isomorphic-ws ws

Then just require this package:

const WebSocket = require('isomorphic-ws');

const ws = new WebSocket('wss://websocket-echo.com/');

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);
};

License

MIT

Package Sidebar

Install

npm i @monsterbitar/isomorphic-ws

Weekly Downloads

255

Version

5.3.1

License

MIT

Unpacked Size

4.15 kB

Total Files

6

Last publish

Collaborators

  • monsterbitar