@mixbee/websocket

1.0.0 • Public • Published

@mixbee/websocket

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 @mixbee/websocket

Usage

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

const WebSocket = require('@mixbee/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

Readme

Keywords

Package Sidebar

Install

npm i @mixbee/websocket

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

5.99 kB

Total Files

5

Last publish

Collaborators

  • stevenshen