websocket-router-server
v0.0.3
Published
websocket router server
Downloads
4
Readme
WebsocketRouter - server
Work in progress Experimental: Don't use for production! Use together with websocket-router-client
Install
npm install websocket-router-server
Example usage
"use strict";
var WebsocketRouter = require("websocket-router-server");
var wr = new WebsocketRouter();
var router = wr.router;
router.post("message", function(socket, request, response) {
response
.tellAll() // Adds all sockets as receivers (default receiver is only sending socket)
.respond(request.data); // responds with same type and route as message came from
});
function printConnectionCount(server) {
console.log("Connection count: "+server.sockets.length);
}
wr.on("connect", function(socket) {
printConnectionCount(wr);
});
wr.on("disconnect", function(code, reason) {
printConnectionCount(wr);
});
wr.createServer();
Docs
Classes
WebsocketRouter
- constructor()
Methods
- createServer([port]) // optional port number, default=4044
- setAllowedOrigins(...origins) // By default all origins are allowed. Allowed origins can be set through a .env file using parameter WEBSOCKET_ORIGIN. Separate all allowed origins with a comma. This method can override prior settings. One string representing every allowed origin.
- on (event, callback) // Registers event listeners (only one listener per event)
Events
- serverready
- servererror
- serverclose
- connect (socket) // socket connected
- disconnect (socket) // socket disconnected
- request (socket, data) // before incoming message is handled
- error // on socket error
Router
- constructor()
Methods
registerRoute (type, route_path, callback) // registers and responses an instance of Route (single route)
get (route_path, callback) // short for registerRoute
post (route_path, callback) // short for registerRoute
put (route_path, callback) // short for registerRoute
delete (route_path, callback) // short for registerRoute
error (route_path, callback) // register error handlers ex: router.error(404, callback) (route not found)
middleware (middlewares) // middlewares needs to be a list of functions and be called before the actual call to route or group
afterware (afterwares) // afterwares needs to be a list of functions and be called before the actual call to route or group
group (presets, callback) // lets you create routes with presets (object). Define subroutes inside callback which gets an instance of Route as parameter.
Route
(Not getting exported but is created through Router calls) Dont use constructor to create instances of this class! Create instances using instance of Router class.
- constructor (router, args, middlewares, afterwares)
Methods
- registerRoute (type, route_path, callback) // registers and responses an instance of Route (single route)
- get (route_path, callback) // short for registerRoute
- post (route_path, callback) // short for registerRoute
- put (route_path, callback) // short for registerRoute
- delete (route_path, callback) // short for registerRoute
- error (route_path, callback) // register error handlers ex: router.error(404, callback) (route not found)
- middleware (middlewares) // registers a list of middlewares, if inside a group middlewares will be chained upon each other
- afterware (afterwares) // registers a list of afterwares, if inside a group afterware will be chained upon each other, afterwares will only be called if all middlewares and callback triggered the next function
- group (presets, callback) // lets you create routes with presets (object). Define subroutes inside callback which gets an instance of Route as parameter.
Route callback format
See middleware pattern for more information on next
function(socket, request, response, next) {}
Response
- constructor (router, socket, args)
Methods
tellSocket () // sets incoming socket as receiver (this is done by default)
addSocket (socket) // adds a socket to receiver list
tellRoom (name) // sets all sockets that belongs ro room as receivers
addRoom (name) // adds all sockets that belongs ro room as receivers
tellAll () // sets all sockets as receivers
respond (data, headers) // sends message to receivers with same type and route as the request came
post (route, data, headers) // sends post response to receivers
get (route, data, headers) // sends get response to receivers
put (route, data, headers) // sends put response to receivers
delete (route, data, headers) // sends delete response to receivers
Socket
Methods
- join (name) // joins room with the name of name :)
- leave (name) // leaves room
- leaveAll () // leaves all rooms
- isinRoom (name) // true if in room, false otherwise
Middleware pattern
All route callbacks receives a socket, request, response and a next function to pass along to the next method in the queue. Example
var cb = function(socket, request, response, next) {
// do something
next(); // call next callback/middleware/afterware in queue
}