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 🙏

© 2025 – Pkg Stats / Ryan Hefner

linenws-server

v1.0.6

Published

An easy-to-use wrapper around the ws api for an express-like feel

Readme

linenws-server

An easy-to-use wrapper around the ws api for an express-like feel. This is an modified version of "easy-websocket-server", this fixes the random crashes/bugs that are annoying

npm i linenws-server

Examples

const app = require('express')();
const server = require('http').createServer(app);
const LinenWS = require('linenws-server');

const { send, broadcastAll } = LinenWS.commands;
const socket = LinenWS(server);

// Listen for connections
socket.onConnection(id => console.log(`${id} just connected!`))

// Listen for someMethod messages
socket.onMessage(`someMethod`, (id, message) => {
    console.log(`${id} sent a message:`, message);
});

// Listen for otherMethod messages
socket.onMessage(`otherMethod`, (id, message) => {
    console.log(`${id} sent a message:`, message);
})

// Send a message to a particular client
send(`someId`, `someMethod`, { ok: true });

// Broadcast to all the clients
broadcastAll(`someMethod`, { ok: true });

server.listen(3000);

Using without express is exactly the same, except you just call:

const server = require('http').createServer();

Instead of this:

const server = require('http').createServer(app);

Remotes

Using the built in remoteEngine (similar to using express.Router());

// index.js
const app = require('express')();
const server = require('http').createServer(app);
const socket = require('linenws-server')(server);
const createUser = require('./remotes/create-user');

socket.useRemote(`createUser`, createUser);

server.listen(3000);
// ./remotes/create-user.js
const remote = require('linenws-server').remoteEngine();

remote.onMessage(id => {
    // ...
    console.log(`User created!`);
})

module.exports = remote;

There is a more thorough example in the /test directory.

Sending messages from the client

All must messages from the client are recomended to have this format:

{
	"method": "someMethod",
	"data": "Whatever you wish to send.  Can be a string, object, or array."
}

If a client sends up a message without a method property, you can assign to the message a default method.

socket.use((id, message, next) => {
    if (!message.method) message.method = `default`;
    next();
})

Then you can handle the message like this:

socket.onMessage(`default`, (id, message) => {
    // Do something...
})

API

const LinenWS = require('linenws-server');

LinenWS(server)

const socket = LinenWS(server);

The server should be a http/https server instance.

socket.onServerUpgrade(callback: (next, quit, request, socket, head) => void)

The callback is called every time when the server upgrades a http(s) request to a websocket.

To keep the request from executing further, call quit(), otherwise you can call next().

Example:

socket.onServerUpgrade((next, quit, request, socket, head) => {
    if (someCondition) next();
    else quit();
})

socket.onConnection(callback: id => void)

The callback is called everytime a new websocket connection is recieved. If quit or next are not called in socket.onServerUpgrade, then this will happen right after onServerUpgrade.

The id is the random id assigned to the client.

socket.use(callback: (id, message, next) => void)

Custom middleware, just like express.use()

socket.onMessage(method: String, callback: (id, message) => void)

The callback is called everytime a message is recieved whose method property equals method.

socket.onClose(callback: (id, code) => void)

The callback is called whenever a websocket connection closes. id will be the id of the connection that closed.

socket.useRemote(method: String, remote: remoteEngine)

The remote is the remoteEngine to use for the given method.

Now LinenWS will call the onMessage method on the remote when the message's method property equals method.

Here is an example.

socket.catchErrors(callback: (error, id, message) => void)

The callback is called every time an unhandled error or promise rejection occurs in the message processing pipeline. message and id are the message that was being processed when the error occured, and the id of the client that sent that message.

LinenWS.commands

const { commands } = LinenWS;

commands.send(id, method, data)

The id is the id of the client to send the message to.

The method is the method of the message.

The data is what is to me sent in the message.

commands.broadcastAll(method, data)

Same as commands.send, except that it broadcasts a message to all the clients.

commands.broadcastExclude()]

The id is the id of the client to exclude.

Otherwise, id is the same as commands.broadcastAll.

commands.close(id?)

If and id is given, LinenWS will close only that connection, otherwise, it will close all the connections.

commands.getConnections(): Object

Returns an object of all the connections. Here is an sample of what that might look like:

{
    "socketId1568846090925": {
        "id": "socketId1568846090925",
        "incommingMessages": [
            {
                "method": "default",
                "data": "this is fake"
            }
        ],
        "outgoingMessages": [
            {
                "method": "someMethod",
                "data": "so is this"
            }
        ],
        "ip": "::1"
    }
}

LinenWS.remoteEngine()

const remote = require('linenws-server').remoteEngine();

// Do something...

module.exports = remote;

See this example.

remote.onMessage(callback: (id, message) => void)

The callback will be called when a message is recieved with a method that matches the method paramater in the socket.useRemote function.

LinenWS.hasConnected(id?): Boolean

Checks if a connection exists.

The id is the id of the connection to check. If no id is given, LinenWS will check for at least one connection.

Thanks

Many thanks to @websockets/ws, the backbone of this package.

License

MIT