Socket
Socket
Sign inDemoInstall

isomorphic-ws

Package Overview
Dependencies
4
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    isomorphic-ws

Isomorphic implementation of WebSocket


Version published
Weekly downloads
5.8M
increased by1.76%
Maintainers
1
Install size
3.95 kB
Created
Weekly downloads
 

Package description

What is isomorphic-ws?

The isomorphic-ws npm package provides a WebSocket client that works both in the browser and on the server (Node.js). It is designed to offer a consistent WebSocket API so that developers can write code that is agnostic to the environment in which it runs. This is particularly useful for building isomorphic JavaScript applications that can run on both the client-side and server-side without modification.

What are isomorphic-ws's main functionalities?

Creating WebSocket connections

This feature allows you to create WebSocket connections to a server. The code sample demonstrates how to establish a connection, and handle open, message, and close events.

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

const ws = new WebSocket('ws://www.host.com/path');

ws.onopen = function () {
  console.log('WebSocket is open now.');
};

ws.onmessage = function (event) {
  console.log('Received message: ' + event.data);
};

ws.onclose = function () {
  console.log('WebSocket is closed now.');
};

Sending messages through WebSocket

This feature allows you to send messages to the server over the WebSocket connection. The code sample shows how to send a message to the server once the WebSocket connection is established and open.

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

const ws = new WebSocket('ws://www.host.com/path');

ws.onopen = function () {
  ws.send('Hello Server!');
};

Receiving messages from WebSocket

This feature allows you to receive messages from the server over the WebSocket connection. The code sample demonstrates how to listen for messages from the server and log them to the console.

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

const ws = new WebSocket('ws://www.host.com/path');

ws.onmessage = function (event) {
  console.log('Received message: ' + event.data);
};

Other packages similar to isomorphic-ws

Changelog

Source

5.0.0 (June 27, 2022)

  • Changed browser to es modules (@guillemcordoba in #20)

Readme

Source

isomorphic-ws

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

License

MIT

Keywords

FAQs

Last updated on 27 Jun 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc