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

chat-app-server

v0.6.0

Published

Simple backend server for a chat application built with Fastify and Socket.IO

Downloads

39

Readme

Chat Application Backend Documentation

This is a simple yet powerful backend server for a chat application, built using Fastify and Socket.IO. It handles user authentication, channel management, and real-time communication with ease.

🔗 Frontend Example Repository: Chat App Frontend

🚀 Installation

To install the necessary package, use the following command:

npm i -s chat-app-server

🏃 Running the Server

To start the server, run:

npx start-server

🔧 Options for start-server

Usage: start-server [OPTIONS]

Options:
  -a, --address <address>  Specify the address to listen on (default: "0.0.0.0")
  -p, --port <port>        Specify the port to listen on (default: 5001)
  -s, --static <path>      Path to static asset files (default: "./build")
  -r, --rule <rule>        Set server rules as a comma-separated list:
                           - First value: Allow all users to delete/rename channels (true/false)
                           - Second value: Allow all users to edit messages (true/false)
                           (default: "true,true")
  -h, --help               Display help for the command

⚙️ Rule Settings (-r or --rule)

The -r or --rule option accepts a pair of boolean values to configure server behavior.

  1. Channel Editing Rule

    • true: All users can delete or rename channels.
    • false: Only admins can delete or rename channels.
  2. Message Editing Rule

    • true: All users can edit their own messages.
    • false: Only admins can edit messages.

Example:

npx start-server -a 127.0.0.1 -p 8080 -r false,true

🔑 User Management API

1. Create New User

Endpoint: POST /api/account/signup

Example Request:

axios.post('/api/account/signup', { username: 'user', password: '123456' })
  .then((response) => {
    console.log(response.data); // => { token: ... }
  });

2. Login

Endpoint: POST /api/account/login

Example Request:

axios.post('/api/account/login', { username: 'user', password: '123456' })
  .then((response) => {
    console.log(response.data); // => { token: ... }
  });

3. Delete

Endpoint: DELETE /api/account/:username

Example Request:

// Admins can delete any user account. Regular users can only delete their own.
axios.delete('/api/account/user', {
    headers: {
      Authorization: `Bearer ${token}`,
    },
  }
).then((response) => {
  console.log(response.data); // Example response: { username: 'user' }
});

4. Rename

Endpoint: PATCH /api/account/:username

Example Request:

// Admins can renames any user account. Regular users can only rename their own.
axios.patch('/api/account/user', { username: 'newName' }, {
    headers: {
      Authorization: `Bearer ${token}`,
    },
  }
).then((response) => {
  console.log(response.data); // Example response: { username: 'newName' }
});

📺 Channel Management API

1. Get Channels

Endpoint: GET /api/channels

Example Request:

axios.get('/api/channels', {
  headers: {
    Authorization: `Bearer ${token}`,
  },
}).then((response) => {
  console.log(response.data); // => [{ id: '1', name: 'general', removable: false }, ...]
});

2. Add Channel

Endpoint: POST /api/channels

Example Request:

const newChannel = { name: 'new channel' };

axios.post('/api/channels', newChannel, {
  headers: {
    Authorization: `Bearer ${token}`,
  },
}).then((response) => {
  console.log(response.data); // => { id: '3', name: 'new channel', removable: true }
});

3. Edit Channel

Endpoint: PATCH /api/channels/:id

Example Request:

const editedChannel = { name: 'new name channel' };

axios.patch('/api/channels/3', editedChannel, {
  headers: {
    Authorization: `Bearer ${token}`,
  },
}).then((response) => {
  console.log(response.data); // => { id: '3', name: 'new name channel', removable: true }
});

4. Remove Channel

Endpoint: DELETE /api/channels/:id

Example Request:

axios.delete('/api/channels/3', {
  headers: {
    Authorization: `Bearer ${token}`,
  },
}).then((response) => {
  console.log(response.data); // => { id: '3' }
});

💬 Message Management API

1. Get Messages

Endpoint: GET /api/messages

Example Request:

axios.get('/api/messages', {
  headers: {
    Authorization: `Bearer ${token}`,
  },
}).then((response) => {
  console.log(response.data); // => [{ id: '1', body: 'text message', channelId: '1', username: 'user' }, ...]
});

2. Add Message

Endpoint: POST /api/messages

Example Request:

const newMessage = { body: 'new message', channelId: '1' };

axios.post('/api/messages', newMessage, {
  headers: {
    Authorization: `Bearer ${token}`,
  },
}).then((response) => {
  console.log(response.data); // => { id: '1', body: 'new message', channelId: '1', username: 'user' }
});

3. Edit Message

Endpoint: PATCH /api/messages/:id

Example Request:

const editedMessage = { body: 'new body message' };

axios.patch('/api/messages/1', editedMessage, {
  headers: {
    Authorization: `Bearer ${token}`,
  },
}).then((response) => {
  console.log(response.data); // => { id: '1', body: 'edited message', channelId: '1', username: 'user' }
});

4. Remove Message

Endpoint: DELETE /api/messages/:id

Example Request:

axios.delete('/api/messages/3', {
  headers: {
    Authorization: `Bearer ${token}`,
  },
}).then((response) => {
  console.log(response.data); // => { id: '3' }
});

📡 Socket.IO Event Subscriptions

Set up Socket.io


const socket = io({
  extraHeaders: {
    authorization: `Bearer ${token}`,
  },
});

1. Subscribe new messages


socket.on('newMessage', (payload) => {
  console.log(payload); // => { id: '2', body: 'edited message', channelId: '1', username: 'user' }
});

2. Subscribe new channel


socket.on('newChannel', (payload) => {
  console.log(payload) // { id: '6', name: "new channel", removable: true }
});
  

3. Subscribe remove channel



socket.on('removeChannel', (payload) => {
  console.log(payload); // { id: '6' };
});

4. Subscribe rename channel


socket.on('renameChannel', (payload) => {
  console.log(payload); // { id: '7', name: "new name channel", removable: true }
});

🛠️ Tech Stack

Backend

  • Fastify: A fast and low-overhead web framework for Node.js, perfect for building high-performance APIs.
  • fastify-socket.io: A Fastify plugin that integrates with Socket.IO for real-time communication via WebSockets.
  • lodash: A utility library providing helpful methods for working with arrays, objects, and functions, making code more concise and easier to manage.
  • commander: A package for building command-line interfaces (CLI) with ease, enabling interaction with Node.js applications through the terminal.
  • http-errors: A simple utility for creating HTTP error objects to standardize error handling in APIs.
  • socket.io: A library that enables real-time, bi-directional communication between clients and servers via WebSockets.

Development Tools

  • ESLint: A tool for identifying and fixing problems in JavaScript code, helping to maintain code quality and consistency.

📝 License

This project is licensed under the MIT License, giving you the freedom to use, modify, and distribute the application as needed.


Developed with ❤️ by Stephan