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

socketry

v2.0.0

Published

A socket library to help with your websocket needs!

Downloads

5

Readme

Docs

About

Socketry allows for an easy way to set up websocket servers and clients. It is built to allow anybody to easily set up websockets without any hastle.

-

Use

Socketry has an easy way to access all of the clients connected to the server.

const Socket = require('./index.js');
const server = new Socket.Server(8080);

console.log(server.clients); // array

You can filter through these clients to target the current client as well, since each client has their own unique id. You can find it by just using client.id.

-

Basic server file

To start using socketry, you will need a server file.

const Socket = require('./index.js');
// Create a new server on port `8080`
const server = new Socket.Server(8080);
// Create a room that messages will be sent to (can have multiple rooms)
const room = new server.Room('foo');
const room2 = new server.Room('bar');

// Fires when a client connects to the server
server.on('connection', usr => {
    console.log('User connected!');
    
    // Fires when the client closes or disconnects from the server
    usr.on('end', extUsr => {
        console.log('User Disconnected');
    });
    
    // fires when a message is sent to the server
    usr.on('message', (data, room) => {
        // Parse the data so that you can use each part
        data = JSON.parse(data);
        console.log(data);
        // Sends the message back to every client in the room
        room.clients.forEach(c => {
            c.send(`${data.usr}: ${data.msg}`);
        });
    });
});

This is just the barebones of a server. It just receives the websockets, and sends them out again to each client.

-

Basic client file

You will also need a way to communicate to the server. You can do this in node by using another barebones file.

const Socket = require('./index.js');
// Create a client that connects to localhost, on port `8080`
const socket = new Socket.Client('ws://localhost:8080');

// Fires when connected to a server
socket.on('open', usr => {
    // Join a room
    socket.joinRoom('foo');
    socket.joinRoom('bar');

    console.log('Connected!');

    // Fires when the client joins the room
    socket.on('join', room => {
        console.log('Joined Room');
        // Fires when server sends a message to the client in the room
        room.on('message', (msg) => {
            console.log(msg);
        });

        // Send a message to the server in object form.
        socket.send({msg: 'Message', usr: 'SharkFin'});

        // Leave the room
        room.leave();

        // Close the websocket connection 
        // socket.end();
    });

    // Fires when the client leaves a room
    socket.on('leave', room => {
        console.log('Left Room');
    });
});

Rooms

The websockets are split into rooms. This means that you can have something that acts as two or more servers, when it's really just one! This means you can host the socket server on one node process. The server can do whatever it wants with the rooms messages. When a message is recieved, the server can send it to all teh clients in the room, just a few clients, or to every room. This would be good for if you wan't to make announcements to every room about an event or such.

.send()

When sending with the client, you send an object. You can have any property inside the object, as long as the server will accept it.