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

especial

v0.1.0-beta-3

Published

Websocket based communication protocol

Downloads

1

Readme

especial build-badge coverage-badge

A websocket based communication protocol.

Usage

npm install especial

Server

const especial = require('especial')

const app = especial()

app.handle('utils.ping', (data, send, next) => {
  send('pong')
})

const server = app.listen(4000, () => {
  console.log(`Listening on port 4000`)
})

Client

const EspecialClient = require('especial/client')

const client = new EspecialClient('ws://localhost:4000')
const { data, message, status } = await client.send('utils.ping')

console.log(message) // "pong"

Protocol

Especial communicates across websockets using JSON encoded payloads. Clients emit a payload with a unique id and the server is expected to respond with a single message with the same id.

Request

Requests are structured like this:

{
  "_rid": "jki7uo9XsOEJkel3PrF_T",
  "route": "utils.ping",
  "data": {}
}

The fields are as follows:

  • _rid: A unique identifier for the request
  • route: A string indicating the function to execute
  • data: An object containing arbitrary data for the function

Response

Responses are structured like this:

{
  "_rid": "jki7uo9XsOEJkel3PrF_T",
  "route": "utils.ping",
  "data": {},
  "message": "pong",
  "status": 0
}
  • _rid: The same _rid emitted in the matching request
  • route: The route requested
  • data: Arbitrary data returned from the function
  • message: String info about function execution
  • status: Integer representing execution result, 0 indicates success

Broadcast

In addition to simple request/response communication servers may send data to clients without a request being made. This can be used to update data or provide new information.

The structure of such a message is the same as a response, with the _rid being a simple string the client may subscribe to.

API

Client

constructor(url, WebSocket): Create a new Especial client

  • url, a websocket url to connect to
  • WebSocket, the websocket implementation, defaults to the global WebSocket variable

connect(options): Promise<void>: Connect to the server, resolves upon success, rejects once the number of retries has been exceeded. This promise may be pending for a long time depending on retry settings.

  • options: An object specifying how reconnects should be performed
  • options.retries: How many times to retry before aborting (default: 3)
  • options.reconnect: Whether failed connections should automatically try to reconnect (default: true)
  • options.retryWait: How long to wait between retry attempts in milliseconds (default: 2000)

addConnectedHandler(fn): id: Register a function that will be called when the client connects or disconnects from a server.

  • fn: A function to be called (no arguments are passed)
  • id: The function returns a handler id that can be used to remove the handler

clearConnectedHandler(id): void: Unregister a connection listener function.

  • id: A handler id from addConnectedHandler.

send(route, data): Promise<Response>: Send a message to the server.

  • route: A string route to call on the server
  • data: An optional object to pass to the server. This data must be JSON serializable
  • returns: Returns a payload structured like the following:
{
  _rid: "jki7uo9XsOEJkel3PrF_T", // the request identifier
  route: "utils.ping", // the requested route
  data: {}, // any data sent from the server
  message: "", // a response string (defaults to 'Success' or 'Failure')
  status: 0 // a response status, 0 indicates success
}

disconnect(): Disconnect from a server and cancel any pending retry requests.

listen(_rid, fn): string: Listen to a custom request id. This can be used for asymmetric communication (e.g. subscribing to server events)

  • _rid: A string to listen to. Any requests emitted from the server with this _rid will cause fn to be executed
  • fn: A function to call when a message with _rid is received. This function is called with a response payload as the only argument
  • returns: Returns a listener id that can be used to clear the listener

clearListener(_rid, listenerId): Clear a route listener.

  • _rid: The route identifier to clear
  • listenerId: The listener id to clear

Server

constructor(): Create a new Especial app.

listen(port, cb): Start a websocket server listening on the specified port.

  • port: The port the server should listen on
  • cb: A callback function to be executed when the server starts. Any error will be passed as the first argument

use(match, fn): Register a middleware function to be used for a set of routes.

  • match: Either a string or regular expression used to determine if the middleware should be executed for a route. If it's a string routes are exactly matched
  • fn: A middleware function to be executed for a route

handle(route, ...handlers): Register functions to be called for a given route.

  • route: A string route to register handlers for. Wildcards are not supported, the route is directly matched during requests
  • handlers: One or more functions to be executed in series for a route

broadcast(_rid, _message, _data): Broadcast a message to all connected clients.

  • _rid: The route id to use for the broadcast
  • _message: The string message to be sent in the payload
  • _data: JSON serializable data to be sent in the payload

broadcastOne(_rid, _message, _data, ws): Broadcast a message to a specific client.

  • _rid: The route id to use for the broadcast
  • _message: The string message to be sent in the payload
  • _data: JSON serializable data to be sent in the payload
  • ws: The websocket for the client that should receive the broadcast