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

chachan

v0.0.6

Published

Socket-based backend for chat with common functionality

Readme

Installation

npm install chachan

Usage

  1. Create an instance of chat(ex. PersistentChatWithRooms)
  2. Set hooks for incomming client connections via ::setClientHooks
  3. Add you custom listeners via ::on
  4. Start server via ::start

Hooks

chachan handles different incomming events via internal commands. In order to make this process customizable, one can provide hooks for command. For example, developer can prepare arguments, that passed into handler with before hook and change output of handler with after hook. All hooks are receiving two arguments: socket for client connection and object with payload. Every hook have to return some data that will replace/update original payload. In such manner, anything that you return from before hook will completely replace original input for event(because it receives original input as second argument), and anything you return from after hook will replace result of event handler(because it receives original result as second argument).

For now, all handlers supports only before and after hooks. They should be registered via:

chatInstance.setClientHooks({handlerName: {before: beforeFunction, after: afterFunction}})

List of handlers and events that are triggering them:

  • userLogin - user:login
  • userLogout - user:logout
  • roomList - room:list
  • roomVisit - room:visit
  • roomDetails - room:details
  • roomCreate - room:create
  • roomJoin - room:join
  • roomLeave - room:leave
  • roomInvite - room:invite
  • roomExpell - room:expell
  • message - message

Client events

  • error:validation - event emitted by user is missing some required param / param has incorrect value
  • room:visited - room was just visited by user(received in response to room:visit)
  • room:joined - user itself joined room(room:join)
  • room:invited - user joined room after invitation (room:invite)

Example

import { PersistentChatWithRooms } from 'chachan';
const chat = new PersistentChatWithRooms(3000);
const Rooms = new Map<string, Set<string>>()
chat.setClientHooks({
  roomCreate: {before: (socket, data = {}) => {
    const {user} = socket.request;
    if (!user) {
      return data;
    }

    const {room} = data

    if (!Rooms.get(user)) {
      Rooms.set(user, new Set)
    }
    Rooms.get(user)!.add(room)
    return data;
  }},
  userLogin: {after: (_socket, {user}={}) => {
    chat.setRooms(user, Array.from(Rooms.get(user)||[]))
    return {user}
  }}

})
chat.start()
<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8"/>
        <title>Document</title>
    </head>
    <body>
        <div id="chat">
            <fieldset>
                <legend>Rooms[<span id="activeRoomLabel"></span>]</legend>
                <input placeholder="Room" onchange="createRoom(this.value); this.value=''"/>
                <button onclick="resetRooms(); fetchRooms()">Reset</button>
                <ul id="rooms" onclick="switchRoom(event)">
                    <li>a</li>
                    <li>b</li>
                </ul>
            </fieldset>
            <input placeholder="Login" onchange="user=this.value; login(); this.value=''"/>
            <br/>
            <input placeholder="Message" onchange="sendMessage(this.value); this.value=''"/>
            <ul id="log"></ul>
        </div>
        <script src="http://localhost:3000/socket.io/socket.io.js"></script>
        <script>
         let chat, user, room;
         window.addEventListener('load', initChat);
         function switchRoom(event) {
             room = event.target.textContent;
             chat.emit('room:visit', {room})
             chat.emit('room:get', {room}, history => {
                 resetLog();
                 history.forEach(({message}) => logChatMessage(message));
             })
             activeRoomLabel.textContent = room;
         }
         function createRoom(room) {
             chat.emit('room:create', {room})
             addRoom(room);
         }

         function sendMessage(message) {
             chat.emit('message', {room, message})
         }

         function login() {
             chat.emit('user:login', {user})
         }
         function resetRooms() {
             document.getElementById('rooms').innerText = ''
         }
         function fetchRooms() {
             chat.emit('room:list', {}, rooms => rooms.forEach(addRoom));
         }

         function resetLog() {
             document.getElementById('log').innerText = ''
         }
         function addRoom(data) {
             const rooms = document.getElementById('rooms')
             const room = document.createElement('li');
             room.textContent = data;
             rooms.append(room);
         }

         function logChatMessage(data) {
             const log = document.getElementById('log')
             const msg = document.createElement('li');
             console.log(data)
             msg.textContent = data;
             log.prepend(msg);
         }
         function initChat() {
             chat = io('http://localhost:3000/chachan/chat', {query: {token: 123}});
             chat.on('message', ({room: to, message}) => to === room && logChatMessage(message));
             chat.on('error', console.error);
             chat.on('reconnect', login);
         }
        </script>
    </body>
</html>