x-platform-ws v0.2.1
x-platform-ws
The cross-platform WebSocket wrapper in Javascript, imitating the API of the ws package on browser.
Installation
First make sure you have node and npm (Comes with Node) installed, then run:
$ npm install x-platform-ws --saveBingo! You're made it!
Further information
If you are x-platform-ws in a project with browserify or webpack, everything will be working fine.
If not, you should add events and buffer to your dependencies. They are peer dependencies of x-platform-ws so you have to install them manually. (Webpack and browserify have them installed and automatically bundled for browsers)
Usage
const WebSocket = require('x-platform-ws');
const ws = new WebSocket('wss://echo.websocket.org');
ws.on('message', data => console.log(data));
ws.on('open', () => console.log('Opened!'));
ws.on('close', (code, reason) => console.log(`Closed: code=${code}, reason=${reason}`));
setTimeout(() => ws.close(), 10000);It is almost compatible with the client in the ws package, with some noticable differences:
- The
optionsparameter of theWebSocketconstructor is only effective on the node side. - The
ping()andpong()methods are not supported in the browser. See this SO thread for explanations. - The UNIX domain sockets are not supported in the browser.
- The
'fragment'binaryTypeis not supported on the browser. - The
'ping','pong','unexpected-response'and'upgrade'events are not supported on the browser. - The
send()method does not take advantage of theoptionsand thecallbackis invoked immediately.
For the ws api, see the official docs.
Features
- Support using
'nodebuffer'asbinaryTypewith almost 100% compatibility. (Thanks to thebuffernpm package) - Support using
on(),once()and other methods of the NodeEventEmitterclass. (Thanks to theeventsnpm package)
Development
First clone the repository:
$ git clone https://github.com/std4453/x-platform-wsThen, change to working dir:
$ cd x-platform-wsAnd:
$ npm installNow play with it as you like!
Versioning
We use semantic versioning for versioning. For the versions available, see the tags on this repository.
Authors
- std4453 - me@std4453.com
License
This project is licensed under the MIT License, see LICENSE for details.