Isomorphic WebSocket client implementation inspired by the heinisuo/isomorphic-ws package.
It uses ws on Node and native WebSocket in browsers.
Before using this module, you should be aware that ws is not fully compatible with the WebSocket API. It may not match the implementation in the browser, or it may introduce its own methods (for example, ping and pong, which are not in the browser version). You should always test your code in both Node and browsers for better compatibility and security.
- There is support for ES modules
- Uses a shorter WebSocket export syntax in the browser, due to almost 100% support for 2022 (for comparison, export in the isomorphic-ws)
You need to install both this package and ws:
npm i modern-isomorphic-ws ws
If you are using TypeScript, also set the type declarations:
npm i @types/ws
Now just import this package:
import WebSocket from "modern-isomorphic-ws";
// And write your code ;)