1.3.1 • Published 7 months ago

simple-websockets-server v1.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Statements CI Dependencies Downloads Version

Simple Websockets Server

What is it for?

It's super easy, super thin server package for event systems in WebSockets to work with simple-websockets (inspired by socket.io but with no bloat).

Example

import { SimpleWebSocketServer } from 'simple-websockets-server';

const server = new SimpleWebSocketServer({ port: 1234 });

server.onConnection((socket: SimpleWebSocket, request: http.IncomingRequest) => {
    socket.on("some event from socket", (someData) => {
        socket.send("some response", someResponseData);
    });
});

server.send("event name to send to all clients", 1, 2, 3, "fourth argument");

Documentation

SimpleWebSocketServer extends from ws.Server, so constructor is precisely the same. SimpleWebSocketServer has 2 additional methods:

  • onConnection(socket => void) - connection listener. socket is an instance of SimpleWebSocket
  • send(eventName, data) - sends event and specified data to all connected sockets

server.send sends to server stringified JSON object

{
    eventName: "event name is the first argument",
    values: []
}

where values is the array of arguments after first argument of send method.

socket.on listens for incoming data that fits the object and calls listener.

You can send events without values.

Event name must be non-empty string.

1.3.1

7 months ago

1.2.0

2 years ago

1.2.1

2 years ago

1.1.0

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago