
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
session-socket
Advanced tools
-The Socket.io session/user management library:
Why session-socket?
npm install session-socket --save
This expression returns an object which contains a variety of different methods for acting on each of your apps active websocket connections.
Returns: [obj]
{
.all(),
.length(),
.get(),
.toArray(),
.connect(),
.disconnect()
}
Example:
session.connections;
Full example:
const io = require('socket.io');
const session = require('session-socket');
io.on('connection', (socket) => {
// Get all active connections:
const allConnections = session.connections.all();
// Get an individual user:
const userObj = session.connections.get(socket);
// Get an array of each active connection:
const arr = session.connections.toArray();
// Get number of active connections:
const countConnections = session.connections.length();
// Connect a new socket:
session.connections.connect(socket);
// Disconnect a socket:
session.connections.disconnect(socket);
});
This method connects a socket instance to the library. In most scenarios, you will want to invoke this method immediately after establishing your websocket connection.
It is the shorthand way of calling session.connections.connect()
.
Arguments([1], [2]):
Returns: [null]
Example:
session.connect(socket, {
name: 'Don',
location: 'Vancouver'
});
Full example:
const io = require('socket.io');
const session = require('session-socket');
io.on('connection', (socket) => {
session.connect(socket);
socket.on('disconnect', () => {
console.log(`User disconnected: ${socket.id}`);
});
});
This method disconnects a socket instance from the library. Call it from within your disconnect handler.
It is the shorthand way of calling session.connections.disconnect()
.
Arguments([1]):
Returns: [null]
Example:
session.disconnect(socket);
Full example:
const io = require('socket.io');
const session = require('session-socket');
io.on('connection', (socket) => {
session.connect(socket);
socket.on('disconnect', () => {
session.disconnect(socket);
console.log(`User disconnected: ${socket.id}`);
});
});
This method takes a socket object as a parameter and returns the corresponding user.
It is the shorthand way of calling session.connections.get()
.
Add any additional properties you wish to set on the user to the object returned by this method.
Arguments([1]):
Returns: [obj]
{
.assign(),
.addProp(),
.removeProp(),
.rooms(),
.roomList(),
.joinRoom(),
.leaveRoom()
}
Example:
session.get(socket);
Full example:
const io = require('socket.io');
const session = require('session-socket');
io.on('connection', (socket) => {
session.connect(socket);
// Get user and assign some initial props:
const user = session.get(socket).assign({ city: 'Vancouver' });
// Modifying the user object:
user.assign({ id: 1, name: 'Don' });
user.addProp('items', {
bike: 'red',
tool: 'hammer',
drink: 'coffee'
});
user.removeProp('items');
// Get an object or array of all joined rooms:
user.rooms();
user.roomList();
// Join and leave a room:
user.joinRoom('chatroom1');
user.leaveRoom('chatroom1');
socket.on('disconnect', () => {
session.disconnect(socket);
console.log(`User disconnected: ${socket.id}`);
});
});
This method takes a name and either an object or function as parameters and stores it on the session
object.
It's a convenience method that let's you write and store custom session/user related plugins for the library.
Arguments([1], [2]):
Returns: [null]
Example:
session.addPlugin('getAndSetLocation', (userObj) => {
// ... some code to figure out their location:
const location = 'Vancouver';
return userObj.location = location;
});
Full example:
const io = require('socket.io');
const session = require('session-socket');
io.on('connection', (socket) => {
session.connect(socket);
session.addPlugin('getAndSetLocation', (userObj) => {
// ... some code to figure out their location:
const location = 'Vancouver';
return userObj.location = location;
});
session.getAndSetLocation(session.get(socket));
console.log(session.get(socket)) // { location: 'Vancouver' }
socket.on('disconnect', () => {
session.disconnect(socket);
console.log(`User disconnected: ${socket.id}`);
});
});
FAQs
The Socket.io session/user management library
The npm package session-socket receives a total of 1 weekly downloads. As such, session-socket popularity was classified as not popular.
We found that session-socket demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.