Pure JavaScript Module for Session Handling NPM


Socket session handler for Socket.IO

NPM Version NPM Downloads Linux Build

Simple Implementation

const app = require('express')()
const http = require('http').Server(app)
const io = require('socket.io')(http)

const session_handler = require('io-session-handler').from(io)

 * Connection returns - Token, ID and the Status (1 - connected, 0 - Disconnected)
 * { id: 'm3aEHZZK2f8WfG3uAAAA', token: '5200cc4a59795529', status: 1 }
session_handler.connectionListener((connection) => {


This array contains all the concurrent sessions session_handler.sessions


The response will be

    "token": "5200cc4a59795529",
    "connections": [
    "data": {
      "lat": 6.836772412061691,
      "lon": 79.87546253018081

Disconnect timeout can be changed using the from method options.

const session_handler = require('io-session-handler').from(io, { timeout: 5000 })

Client Connection

You can connect from Web-Client, iOS or Android

HTML / JavaScript

const socket = io({ query: { token: 'client-token' } })


public Socket connect(String token) {
    IO.Options opts = new IO.Options();
    opts.query = "token=" + token
    Socket mSocket = IO.socket("", opts);
    return mSocket;


This is a Node.js module available through the npm registry.

Before installing, download and install Node.js. Node.js 0.10 or higher is required.

Installation is done using the

$ npm i io-session-handler

Sample Project - Demo

This is a demo project.

Advanced Usage | Push & Broadcast Messages

You can send broadcast message to all client sessions at once.


The push method is capable of sending a push message to only a specific client session and this method returns a boolean value when the session is identified as valid.

let sent = session_handler.pushMessage(client_token, data)

Once the message is delivered to the client, onMessageDelivered method will be triggered with the token of the client and the data it received.

session_handler.onMessageDelivered((data) => {

The data onMessageDelivered contains;

   "data":"Some awesome data I received"

Client Implementation | Push & Broadcast Messages

Once the client received the push message it should emit to push_message_delivery with the data it receives and the token to let the server understand it’s delivered to a certain client.

   "data":"Some awesome data I received"

The client should on the push_message event to receive push messages from server

Android client library is available at SCM - SCMessaging