Home

Awesome

ws-server-wrapper

Lightweight WebSocketServer wrapper lib using ws-wrapper and ws to wrap connected WebSockets. The only dependency is ws-wrapper itself.

Install

npm install ws-server-wrapper

Usage

See ws-wrapper README or the API documentation below for more details.

Quick Server-side Example:

Use ws-server-wrapper to wrap the WebSocket.Server:

const WebSocketServer = require("ws").Server
	, WebSocketServerWrapper = require("ws-server-wrapper");
var wss = new WebSocketServer({port: 3000});
var serverWrapper = new WebSocketServerWrapper(wss);
// Send "msg" event to all connected clients
serverWrapper.emit("msg", "Hello!");
// For all connected clients, listen for the "ping" request on the channel "pointless"
serverWrapper.of("pointless").on("ping", function() {
  // `this` refers to the "pointless" channel for the socket who sent the "ping" request
  // Let's just respond to the request with the value "pong"
  return "pong";
});

API

Class: WebSocketServerWrapper

A WebSocketServerWrapper simply wraps around a WebSocket.Server to give you well-deserved functionality. :)

server = new WebSocketServerWrapper(webSocketServerInstance[, options]);

Constructs a new WebSocketServerWrapper, and binds it to the native WebSocketServer instance from ws.

Events

The EventEmitter-like API looks like this:

See the ws-wrapper API documentation for more details.

Note: server.once() and server.request() are not supported at this time.

Channel API:

Other methods and properties:

Detecting Broken Connections

The WebSocketServerWrapper will automatically (by default) ping all open sockets on a regular basis. If there is no "pong" response by the start of the next ping, the connection will be assumed to be "broken" and will be terminated automatically.
See options.heartbeatInterval for more information. Also see the approached outlined here.