Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/heineiuo/isomorphic-ws
Isomorphic implementation of WebSocket (https://www.npmjs.com/package/ws)
https://github.com/heineiuo/isomorphic-ws
browser isomorphic nodejs websocket websockets ws
Last synced: about 6 hours ago
JSON representation
Isomorphic implementation of WebSocket (https://www.npmjs.com/package/ws)
- Host: GitHub
- URL: https://github.com/heineiuo/isomorphic-ws
- Owner: heineiuo
- License: mit
- Created: 2017-10-05T04:22:24.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-06-20T06:12:50.000Z (over 1 year ago)
- Last Synced: 2025-01-24T10:03:34.907Z (7 days ago)
- Topics: browser, isomorphic, nodejs, websocket, websockets, ws
- Language: JavaScript
- Homepage:
- Size: 111 KB
- Stars: 403
- Watchers: 8
- Forks: 45
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-nodejs - isomorphic-ws - Isomorphic implementation of WebSocket. ![](https://img.shields.io/github/stars/heineiuo/isomorphic-ws.svg?style=social&label=Star) (Repository / Real-time)
- awesome-list - isomorphic-ws
README
# isomorphic-ws
Isomorphic implementation of WebSocket.
It uses:
- [ws](https://github.com/websockets/ws) on Node
- [global.WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) in browsers## Limitations
Before using this module you should know that
[`ws`](https://github.com/websockets/ws/blob/master/doc/ws.md#class-websocket)
is not perfectly API compatible with
[WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/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`](https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketaddress-protocols-options)
argument in browsers## Usage
You need to install both this package and [ws](https://github.com/websockets/ws):
```
> npm i isomorphic-ws ws
```Then just require this package:
```js
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](LICENSE)