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

@emsa16/chat-server

v5.0.2

Published

WebSocket + Express chat server

Downloads

7

Readme

Chat server

npm Build Status Maintainability Test Coverage

A broadcast chat server that uses Express and Websocket through ws.

The chat uses a broadcast subprotocol, however there is also a simple echo subprotocol available (which is also the default protocol).

Using the broadcast subprotocol requires that a nickname is added in the URL when connecting to the server, in the form of '?nickname=NICK'.

Installation

$ npm install @emsa16/chat-server

Running the server

Include the following lines to run the chat server in your application:

var chatServer = require('@emsa16/chat-server');
chatServer.start([dbwebbPort] [, wsServerUrl] [, wsLimitClientTo] [, authMod] [, dbMod] [, chatType]);

The arguments to chatServer.start() are all optional and can be used to set the following:

  • dbwebbPort - Set server port (default: 1337)
  • wsServerUrl - Set server URL (default: ws://localhost:1337)
  • wsLimitClientTo - Set if wanting to block connections from anywhere else than specific client URL (default: false - allows all client URLs)
  • authMod - inject authentication module (default: empty)
  • dbMod - inject database module (default: empty)
  • chatType - indicate what kind of chat server is needed. Currently implemented types are "default-chat" and "game-chat" (default: "default-chat")

To run it as a separate process and with its output in a separate terminal, the above code can be put in a separate file, e.g. chat-server.js and run by executing a command like node chat-server.js.

When starting the process from the command line, the following environment variables can be set by adding these:

  • WS_DBWEBB_PORT=XXXX - Set server port (default: 1337)
  • WS_SERVER_URL="URL" - Set server URL (default: ws://localhost:1337)
  • WS_LIMIT_CLIENT_TO="URL" - Set if wanting to block connections from anywhere else than specific client URL (default: "")

Another option for running the chat server as a separate service is to install it from the Github repo directly:

$ git clone https://github.com/emsa16/chat-server
$ cd chat-server
$ npm install

Now one of the following commands can be entered:

$ npm start                 # Runs server in development mode
$ npm run production        # Runs server in production mode

Chat protocol

Commands

  • message
    • Sends a message to all other users connected to chat
    • parameters: message
  • nick
    • Changes nickname
    • parameters: nickname
  • move (for game chats only)
    • Sends updated position
    • Also sends user image model if a database is being used to store that
    • parameters: position

Example message

{
    "command": "message",
    "params": {
        "message": "My first message"
        }
}    

Response format:

{
    "timestamp": TIMESTAMP,        # server timestamp
    "origin": "user" or "server"   # was the message sent from a user or the server?
    "nickname": NICKNAME           # nickname of message author
    "data": DATA                   # message content
}    

Authentication

It is possible to add an optional authentication module which checks during connection attempts if the user's token is valid. It is done during the startup phase of the chat server (see Running the server above).

To be compatible with this chat module, the auth module has to be a token-based authentication system, needs to have an asynchronous function called checkTokenDirect(token) that returns an object containing a status key indicating authentication status, and tokens must be sent as a URL parameter named token, i.e. ?token=TOKEN.

Database suppport

It is also possible to add a database module during the startup of the server, which is used if additional data about the chat users should be stored in a database. This is currently only used in game chats.

To be compatible with this chat module, the db module needs to have asynchronous find and updateOne methods. It is strongly recommended to use a Mongo database module.

Testing

The following command runs the test suite, which consists of linters (stylelint and eslint) and Jest unit tests:

$ npm test

wscat is a command line tool that can be used to test the server functionality:

# Has to be installed globally
$ npm install -g wscat                            

# Broadcast subprotocol
# Requires that a nickname is provided as a URL query
$ wscat -c ws://[ADDRESS]?nickname=[NICK] -s 'broadcast'

# Echo subprotocol
$ wscat -c ws://[ADDRESS] -s 'echo'

Client

The client/ folder contains an example for a simple client using plain Javascript.