@chipsgg/openservice-ws-server

1.7.0 • Public • Published

Openservice Websocket Server

ratelimits, rpc, event batching, session management

Config Definition

{
    port: 3000,
    host: '127.0.0.1',
    channels: ['public'],
    rateLimitDuration: 1000,
    rateLimitCount: 10,
    parallel: 1, 
    batchTime: 1, 
    batchLength: 10,
}

Service Example

// service.js
module.exports = (config, services, emit) => {

    const public = {
        ping: ({ ... }) => {
            return 'pong'
        }
    }

    const actions = (sessionid, channel, action, args) => {
        // call remote service
        switch(channel) {
            case 'public': {
                return public(args)
            }
        }
    }

    // create server
    const socket = await Socket(config, { actions }, (event, ...args) => {
        switch(event){
            case 'connect': {}
            case 'disconnect': {}
            case 'err': {}
            default: 
                console.log(event, args)
        }    
    });

    return socket
}

API

socket.publish(channel, topic, args)

Publish events to active channel/topic subscribers.

socket.subscribe(topic, sessionid)

Subscribe a sessionid to events from a topic.

socket.unsubscribe(topic, sessionid)

Un-Subscribe a sessionid from events on a topic.

socket.send(channel, sessionid, args)

Send events to a sessionid on a channel.

socket.close()

Close the websocket server conenction.

socket.closeSession(sessionid)

Forcefully close a connection to a sessionid

socket.hasSession(sessionid)

Check if a sessionid is connected.

socket.listSessions()

List all connected sessionids.

socket.cleanup()

Terminate broken session connections.

Readme

Keywords

none

Package Sidebar

Install

npm i @chipsgg/openservice-ws-server

Weekly Downloads

27

Version

1.7.0

License

MIT

Unpacked Size

15.1 kB

Total Files

8

Last publish

Collaborators

  • tacyarg