README
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 --save
Bingo! 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
options
parameter of theWebSocket
constructor 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'
binaryType
is 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 theoptions
and thecallback
is invoked immediately.
For the ws
api, see the official docs.
Features
- Support using
'nodebuffer'
asbinaryType
with almost 100% compatibility. (Thanks to thebuffer
npm package) - Support using
on()
,once()
and other methods of the NodeEventEmitter
class. (Thanks to theevents
npm package)
Development
First clone the repository:
$ git clone https://github.com/std4453/x-platform-ws
Then, change to working dir:
$ cd x-platform-ws
And:
$ npm install
Now 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.