meteor/websocket-driver-node

Name: websocket-driver-node

Owner: Meteor

Description: WebSocket protocol handler with pluggable I/O

Created: 2013-10-12 02:21:59.0

Updated: 2015-09-04 09:23:41.0

Pushed: 2014-10-29 23:50:18.0

Homepage: null

Size: 449

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

websocket-driver Build Status

This module provides a complete implementation of the WebSocket protocols that can be hooked up to any I/O stream. It aims to simplify things by decoupling the protocol details from the I/O layer, such that users only need to implement code to stream data in and out of it without needing to know anything about how the protocol actually works. Think of it as a complete WebSocket system with pluggable I/O.

Due to this design, you get a lot of things for free. In particular, if you hook this module up to some I/O object, it will do all of this for you:

This library was originally extracted from the Faye project but now aims to provide simple WebSocket support for any Node-based project.

Installation
m install websocket-driver
Usage

This module provides protocol drivers that have the same interface on the server and on the client. A WebSocket driver is an object with two duplex streams attached; one for incoming/outgoing messages and one for managing the wire protocol over an I/O stream. The full API is described below.

Server-side with HTTP

A Node webserver emits a special event for 'upgrade' requests, and this is where you should handle WebSockets. You first check whether the request is a WebSocket, and if so you can create a driver and attach the request's I/O stream to it.

http = require('http'),
websocket = require('websocket-driver');

server = http.createServer();

er.on('upgrade', function(request, socket, body) {
 (!websocket.isWebSocket(request)) return;

r driver = websocket.http(request);

iver.io.write(body);
cket.pipe(driver.io).pipe(socket);

iver.messages.on('data', function(message) {
console.log('Got a message', message);
;

iver.start();

Note the line driver.io.write(body) - you must pass the body buffer to the socket driver in order to make certain versions of the protocol work.

Server-side with TCP

You can also handle WebSocket connections in a bare TCP server, if you're not using an HTTP server and don't want to implement HTTP parsing yourself.

The driver will emit a connect event when a request is received, and at this point you can detect whether it's a WebSocket and handle it as such. Here's an example using the Node net module:

net = require('net'),
websocket = require('websocket-driver');

server = net.createServer(function(connection) {
r driver = websocket.server();

iver.on('connect', function() {
if (websocket.isWebSocket(driver)) {
  driver.start();
} else {
  // handle other HTTP requests
}
;

iver.on('close', function() { connection.end() });
nnection.on('error', function() {});

nnection.pipe(driver.io).pipe(connection);

iver.messages.pipe(driver.messages);


er.listen(4180);

In the connect event, the driver gains several properties to describe the request, similar to a Node request object, such as method, url and headers. However you should remember it's not a real request object; you cannot write data to it, it only tells you what request data we parsed from the input.

If the request has a body, it will be in the driver.body buffer, but only as much of the body as has been piped into the driver when the connect event fires.

Client-side

Similarly, to implement a WebSocket client you just need to make a driver by passing in a URL. After this you use the driver API as described below to process incoming data and send outgoing data.

net = require('net'),
websocket = require('websocket-driver');

driver = websocket.client('ws://www.example.com/socket'),
tcp = net.createConnection(80, 'www.example.com');

pipe(driver.io).pipe(tcp);

er.messages.on('data', function(message) {
nsole.log('Got a message', message);


on('connect', function() {
iver.start();

Client drivers have two additional properties for reading the HTTP data that was sent back by the server:

Driver API

Drivers are created using one of the following methods:

er = websocket.http(request, options)
er = websocket.server(options)
er = websocket.client(url, options)

The http method returns a driver chosen using the headers from a Node HTTP request object. The server method returns a driver that will parse an HTTP request and then decide which driver to use for it using the http method. The client method always returns a driver for the RFC version of the protocol with masking enabled on outgoing frames.

The options argument is optional, and is an object. It may contain the following fields:

A driver has two duplex streams attached to it:

All drivers respond to the following API methods, but some of them are no-ops depending on whether the client supports the behaviour.

Note that most of these methods are commands: if they produce data that should be sent over the socket, they will give this to you by emitting data events on the driver.io stream.

driver.on('open', function(event) {})

Sets the callback to execute when the socket becomes open.

driver.on('message', function(event) {})

Sets the callback to execute when a message is received. event will have a data attribute containing either a string in the case of a text message or a Buffer in the case of a binary message.

You can also listen for messages using the driver.messages.on('data') event, which emits strings for text messages and buffers for binary messages.

driver.on('error', function(event) {})

Sets the callback to execute when a protocol error occurs due to the other peer sending an invalid byte sequence. event will have a message attribute describing the error.

driver.on('close', function(event) {})

Sets the callback to execute when the socket becomes closed. The event object has code and reason attributes.

driver.setHeader(name, value)

Sets a custom header to be sent as part of the handshake response, either from the server or from the client. Must be called before start(), since this is when the headers are serialized and sent.

driver.start()

Initiates the protocol by sending the handshake - either the response for a server-side driver or the request for a client-side one. This should be the first method you invoke. Returns true iff a handshake was sent.

driver.parse(string)

Takes a string and parses it, potentially resulting in message events being emitted (see on('message') above) or in data being sent to driver.io. You should send all data you receive via I/O to this method by piping a stream into driver.io.

driver.text(string)

Sends a text message over the socket. If the socket handshake is not yet complete, the message will be queued until it is. Returns true if the message was sent or queued, and false if the socket can no longer send messages.

This method is equivalent to driver.messages.write(string).

driver.binary(buffer)

Takes a Buffer and sends it as a binary message. Will queue and return true or false the same way as the text method. It will also return false if the driver does not support binary messages.

This method is equivalent to driver.messages.write(buffer).

driver.ping(string = '', function() {})

Sends a ping frame over the socket, queueing it if necessary. string and the callback are both optional. If a callback is given, it will be invoked when the socket receives a pong frame whose content matches string. Returns false if frames can no longer be sent, or if the driver does not support ping/pong.

driver.close()

Initiates the closing handshake if the socket is still open. For drivers with no closing handshake, this will result in the immediate execution of the on('close') driver. For drivers with a closing handshake, this sends a closing frame and emit('close') will execute when a response is received or a protocol error occurs.

driver.version

Returns the WebSocket version in use as a string. Will either be hixie-75, hixie-76 or hybi-$version.

driver.protocol

Returns a string containing the selected subprotocol, if any was agreed upon using the Sec-WebSocket-Protocol mechanism. This value becomes available after emit('open') has fired.

License

(The MIT License)

Copyright (c) 2010-2013 James Coglan

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.