Giter VIP home page Giter VIP logo

isomorphic-ws's Introduction

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://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

isomorphic-ws's People

Contributors

4ian avatar anders94 avatar dependabot[bot] avatar guillemcordoba avatar heineiuo avatar ineiti avatar julien-f avatar lpinca avatar lukehorvat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

isomorphic-ws's Issues

ESM Import Error

I keep getting the below error when trying to use this package with Deno via ESM.SH.

/* esm.sh - error */
throw new Error("[esm.sh] " + "parseCJSModuleExports: Can't resolve 'ws' in '/tmp/esm-build-22c48223b295c5ba9976ebd601dab8cc371bc315-87cf645d/node_modules/isomorphic-ws'");
export default null;

Consider marking the `ws` peer dependency as optional

According to the peerDependencies documentation, NPM 7 will auto-install peerDependencies if they aren't explicit dependencies of the application. This means that an in-browser application that depends on this library will end up downloading the ws library even though it's not needed.

In my opinion, we should mark ws as optional using peerDependenciesMeta, and document that node.js applications should make sure to include ws as one of their dependencies. We'll probably also want a runtime check that the ws library is present, and throw an Error with a helpful message if it's not.

Add support for normal "ws" events

Can you add the event listener functionality of ws and extend the WebSocket object? That way your code could really be isomorphic?

So, for instance, the on('message') event instead of the onmessage callback

Version 5.0.0 fails when used with typescript and karma

16 12 2022 11:18:24.725:INFO [compiler.karma-typescript]: Compiling project using Typescript 4.9.4
16 12 2022 11:18:29.280:INFO [compiler.karma-typescript]: Compiled 26 files in 4529 ms.
16 12 2022 11:18:30.135:ERROR [validator.karma-typescript]: Invalid syntax in bundle: 'import' and 'export' may only appear at the top level (23:0)
in /var/folders/mn/61rhh3pd1fscx3hrwc0wvpy40000gn/T/karma-typescript-bundle--56131-pidm5G34zm0L-.js
at line 23, column 0:

... export default ws

},'node_modules/isomorphic-ws/b ...

Way to compile without error

Hi there,

Is there a way to compile using webpack so in the case of browser compilation, it does not throw errors because it tries to compile ws too ?

WARNING in ./node_modules/ws/lib/buffer-util.js
    Module not found: Error: Can't resolve 'bufferutil' in '/.../node_modules/ws/lib'
     @ ./node_modules/ws/lib/buffer-util.js 35:21-42
     @ ./node_modules/ws/lib/receiver.js
     @ ./node_modules/ws/index.js
     @ ./node_modules/isomorphic-ws/node.js
     ....
     @ ./index.js

    WARNING in ./node_modules/ws/lib/validation.js
    Module not found: Error: Can't resolve 'utf-8-validate' in '/.../node_modules/ws/lib'
     @ ./node_modules/ws/lib/validation.js 10:22-47
     @ ./node_modules/ws/lib/receiver.js
     @ ./node_modules/ws/index.js
     @ ./node_modules/isomorphic-ws/node.js
     ....
     @ ./index.js

It works fine when compiling for node.

Thanks,

Nicolas

LICENSE

Could you please add a license to your project? While the package.json states it is MIT licensed, adding a copy of the MIT license into the repository would be wonderful.

Error: 'WebSocket': The subprotocol '[object Object]' is invalid.\n at RPC.createSocket

Error during communication with engine {"stack":"Error: Failed to construct 'WebSocket': The subprotocol '[object Object]' is invalid.\n at RPC.createSocket (http://localhost:4200/**/apps/**/ma

signedToken = jwt.sign(this.token, this.key, { algorithm: 'RS256' });

config = {
schema: this.schema,
url: wss://${this.senseHost}/${this.proxyPrefix}/app/engineData,
// Notice how the signed JWT is passed in the 'Authorization' header using the
// 'Bearer' schema:
createSocket: (url) => new WebSocket(url,
{
headers: { Authorization: 'Bearer ' + this.signedToken }
})
};

createEnigmaSession() {
let session;
if (session) {
session.close();
}

session = enigma.create(this.config);
session.on('traffic:*', (dir, data) => console.log(dir, data));
return session;

}

plans for ws 8.x?

In the ws npm package, major version 8 brings some significant divergence from browser web sockets, in that messages with text data now present the data as a Buffer instead of a string. See here websockets/ws@e173423

I'm wondering if isomorphic-ws will do something to reconcile this with the browser version that gives a string.

It's alive? ๐Ÿ˜Ÿ

It appears that support for this package has been discontinued. It's a pity because there are not enough actual edits to support ES modules.
It will be great if the work in this repository resumes.
Just in case, I made my package with some edits, you never know it will be useful to someone.

Please export Websocket

ws provides the named export "WebSocket".

Please export this in browser.js too.

Error: export 'WebSocket' (imported as 'WebSocket') was not found in 'isomorphic-ws' (possible exports: default)

Export ES modules

This is the error that I get when importing isomorphic-ws and bundling with rollup:

[!] Error: 'default' is not exported by node_modules/isomorphic-ws/browser.js, imported by node_modules/@holochain/conductor-api/lib.es/websocket/client.js
https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
node_modules/@holochain/conductor-api/lib.es/websocket/client.js (10:7)
 8:     });
 9: };
10: import Websocket from "isomorphic-ws";

So you have to add the rollup commonjs plugin to make it work.

I think it would be really good if this library would export es modules as well, it would improve ease of use in a lot of cases, even more so when it only needs to expose the native WebSocket object. I can make a PR if there is good feedback about this.

Can't construct WebSocket

This code fails:

const WebSocket = require("isomorphic-ws");
const ws = new WebSocket("wss://echo.websocket.org/", {
    origin: "https://websocket.org"
});

Firefox:

SyntaxError: An invalid or illegal string was specified

Chromium-based browser:

Uncaught DOMException: Failed to construct 'WebSocket': The subprotocol '[object Object]' is invalid.
    at Object.76.isomorphic-ws (https://localhost:9966/index.js:33990:12)
    at o (https://localhost:9966/index.js:1:265)
    at r (https://localhost:9966/index.js:1:431)
    at https://localhost:9966/index.js:1:460

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.