5.0.2 • Published 6 months ago

isomorphic-gecko v5.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

isomorphic-gecko

WebSocket client with support for Node.js, browsers, and Expo/React Native.

Isomorphic implementation of WebSocket.

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 all target environments (Node, browsers, and Expo).

Some major differences:

  • no Server implementation in browsers or Expo
  • no support for the constructor options argument in browsers or Expo

Installation

Node.js

For Node.js environments, you need to install both this package and ws:

> npm i isomorphic-gecko ws

Browsers and Expo

For browser or Expo environments, you only need to install this package:

> npm i isomorphic-gecko

Usage

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

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

Expo Usage

When using this package in an Expo environment, it will automatically detect and use Expo's WebSocket implementation. No additional configuration is needed.

License

MIT

5.0.2

6 months ago

5.0.1

7 months ago