1.0.1 • Published 7 years ago

websocket-transport v1.0.1

Weekly downloads
8
License
MIT
Repository
github
Last release
7 years ago

websocket-transport

Transport implementation over websockets using JSON serialization. For using both in Node.js and Browser environment.

Read about the Transport concept here.

Usage

npm install -s websocket-transport

Server

const ws = require('ws');
const WebsocketTransport = require('websocket-transport');

const server = new ws.Server({ port: 8080 });
server.on('connection', websocket => {
  console.log('incoming connection');
  const transport = new WebsocketTransport(websocket);
  transport.on('message', message => {
    console.log('message: %j', message);
  });
  transport.on('close', err => {
    console.log('connection closed');
  });
  transport.send({
    foo: 'bar',
  });
});

Client (Node.js)

const ws = require('ws');
const WebsocketTransport = require('websocket-transport');

WebsocketTransport.connect({
  WebSocket: ws,
  url: 'ws://localhost:8080'
}, (err, transport) => {
  if (err)
    return console.log(err.message);
  console.log('connected');
  transport.on('message', message => {
    console.log('message: %j', message);
  });
  transport.on('close', err => {
    console.log('connection closed');
  });
  transport.send({
    cat: 'meow',
  });
});

Client (Browser)

const WebsocketTransport = require('websocket-transport');

WebsocketTransport.connect({
  WebSocket, // native WebSocket from global namespace
  url: 'ws://localhost:8080'
}, (err, transport) => {
  if (err)
    return console.log(err.message);
  console.log('connected');
  transport.on('message', message => {
    console.log('message: %j', message);
  });
  transport.on('close', err => {
    console.log('connection closed');
  });
  transport.send({
    cat: 'meow',
  });
});

WebsocketTransport API

For details and other methods see common transport interface

const WebsocketTransport = require('websocket-transport');

Properties

websocket

Reference to the WebSocket instance passed to the constructor.

Methods

constructor(websocket)

Takes WebSocket instance to attach on. In browser it is the standard WebSocket, and in Node.js it's something compatible with the ws library interface.

send(message, callback)

In Node.js implementation callback will be called after the message has been writed to underlying socket. However, it is impossible to do that in browser, so callback will be called immediately.

Static

WebsocketTransport.connect(options, callback)

options.WebSocket - WebSocket class constructor (native WebSocket in browser or compatible with ws class in Node.js)
options.url - where to connect to
options.protocols - protocols, optional, string or array (see this or that)
options.options - options for Node.js implementation, optional, see ws)