npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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 
}