@ont-community/html5-websocket

HTML5 WebSocket for node.js

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
00Jul 23, 2018Jul 23, 2018Minified + gzip package size for @ont-community/html5-websocket in KB

Readme

html5-websocket

This module implements the standard HTML5 WebSocket API as a thin layer over the great ws library.

The motivation behind this module is to be able to write WebSocket based libraries that will run without changes in Node.js, Browsers or React Native apps.

Install

npm install --save @ont-community/html5-websocket

Usage

The API is the same as the standard HTML5 Socket API so nothing new here...

const WebSocket = require('@ont-community/html5-websocket');

const ws = new WebSocket('ws://html5rocks.websocket.org/echo');

ws.onopen = function () {
    ws.send('Hello!');
};

ws.onmessage = function (e) {
    console.log(e.data);
};

ws.onerror = function (error) {
    console.log('Fail:', error);
};

You can also use the DOM Level 2 Event Model

const handleMessage = function (e) {
    console.log(e.data);
};

// add listener
ws.addEventListener('message', handleMessage);

// remove listener
ws.removeEventListener('message', handleMessage);

License

MIT

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.