I have 4 sockets in a room. How can I link the sockets? I want to use it like this:
socket1 = param1;
socket2 = param2;
socket3 = param3;
socket4 = param4;
allSockets = ???
allSockets.on('', function(...)...)
You can broadcast/emit to all members of a room. I'm not aware of any way to apply a listener that listens only to those sockets in a room without attaching a separate listener to each socket in the room.
I can think of a couple alternative implementations:
If you have an array of sockets that you want to apply listeners to, then you can just iterate through the array and add listeners:
function addListeners(sockets, msg, fn) {
for (var i = 0; i < sockets.length; i++) {
sockets[i].on(msg, fn);
}
}
It might also be possible to add the listener to all sockets, but only process the message if the socket is actually in a particular rooom:
io.on('connection', function(socket) {
socket.on("msg", function(data) {
// here check if this socket is actually in the desired room
// if so, process the message
// if not, ignore the message
});
});
Related
How do I keep track of all the connected clients in socket.io?
I have tried this on the server:
let numSockets = 0;
io.on('connection', (socket) => {
io.sockets.emit('numSockets', ++numSockets);
socket.on('disconnect', () => {
io.sockets.emit('numSockets', --numSockets);
});
});
and this on the client:
const socket = io();
socket.on('numSockets', function (numSockets) {
console.log(numSockets);
});
It does print a number, but the number, however, if I open 2 windows, it shows 4 connected sockets.
Is this the correct way to do it?
What I want to achieve is to print a list of the connected sockets' ids in a sidebar on my website, and let the user set a username (instead of the automatically generated id) if they want to.
But before moving on to this, I will make sure I can keep track of the sockets in a correct way.
I don't think that io.sockets.emit(io.of('/').connected) is a good idea because it will send a hash of socket objects which is a lot of data :-)
You can try the following function:
function findUsersConnected(room, namespace) {
var names = [];
var ns = io.of(namespace || "/");
if (ns) {
for (var id in ns.connected) {
if(room) {
var roomKeys = Object.keys(ns.connected[id].rooms);
for(var i in roomKeys) {
if(roomKeys[i] == room) {
if (ns.connected[id].username) {
names.push(ns.connected[id].username);
} else {
names.push(ns.connected[id].id);
}
}
}
} else {
if (ns.connected[id].username) {
names.push(ns.connected[id].username);
} else {
names.push(ns.connected[id].id);
}
}
}
}
return names.sort();
}
which returns an array of users connected to a room in a namespace. If a socket has not socket.username property then socket.id is used instead.
For instance:
var usersConnected = findUsersConnected();
var usersConnected = findUsersConnected('myRoom');
var usersConnected = findUsersConnected('myRoom', '/myNamespace');
There's the Namespace#connected object that contains all sockets (keyed by their id) that are connected to a particular namespace.
To retrieve the socket id's of the default namespace (/):
let clientIds = Object.keys( io.of('/').connected );
(where io is the server instance)
As of today, socket.io implemented a function called fetchSockets() on server side to retrieve all sockets that are currently connected on the server-side. (Source : https://socket.io/docs/v4/server-instance/#fetchSockets)
You can then use it like this :
const io = require("socket.io")
async function retrieveSockets() {
const connectedSockets = await io.fetchSockets()
}
As far as i tested, you can even execute action on sockets thanks to that, like emitting events, joinings rooms...etc
const count = io.engine.clientsCount;
This seems like a more inclusive approach and may be the same count of Socket instances in the main namespace as below. But depending on namespaces and usage they could be different.
const count2 = io.of("/").sockets.size;
https://socket.io/docs/v4/server-api/#engineclientscount
I believe it is Object.keys(io.sockets.connected).length. Check server api http://socket.io/docs/server-api/
I'm trying to broadcast a message to all users of a room when a socket disconnects
afterDisconnect: function(session, socket, cb) {
var rooms = sails.sockets.socketRooms(socket);
for(var i = 0; i < rooms.length; i++) {
if(rooms[i].indexOf("gameRoom") > - 1) {
sails.sockets.broadcast(rooms[i], 'byeUser', socket.id, socket);
break;
}
}
return cb();
}
Thing is when the event occurs the socket is no longer on any room so... how am I supposed to achieve this? I was thinking making a ping-pong request stuff but no idea where should I put that.
Is it possible to use socket.io to dispatch events to the client and not send them to the server? Something like:
socket.emitLocally('some event', data);
Here is why I'm asking:
At the moment my server emits events to all sockets.
io.sockets.emit()
While this works well, there is the possibility of a delay between the user interaction and the response from the server.
This is why I would prefer to use broadcast on the server side and handle things immediatly in the broadcasting client.
socket.broadcast.emit()
The client is based on angular.js and the modules don't know of each other. I can't access the code that is responsible for updating the client directly. I would have to use some kind of event dispatching service which I would inject into relevant modules.
Since this is basically what I am doing with socket.io right now I wonder if I could not simply use their system to do this.
Thank you for any input or suggestions!
This can be done as shown on the socket.io client code on function Emitter.prototype.emit.
Events are saved on socket._callbacks with the prefix $, so for instance "connected" would be under _callbacks as "$connected".
You can replicate the behaviour of said function by using:
// You can use internal (e.g. disconnected) or custom events (e.g. ChatMessage).
var event = "MyEvent";
// Set your args exactly as in your socket.io server emit.
// Note that they will be used on an apply so they must be within an array.
var args = [1, 2, 3];
if(socket._callbacks) { // If the socket has callbacks
var callbacks = socket._callbacks['$' + event]; // Load the callback event
if (callbacks) {
callbacks = callbacks.slice(0);
for (var i = 0, len = callbacks.length; i < len; ++i)
callbacks[i].apply(socket, args);
} // Otherwise no calls
}
In the example above the callback for "MyEvent" will be called with the arguments 1, 2, 3.
A further example on how to use this on an actual connection:
io.on('connection', function (socket) {
socket.on("getSingleUpdate", function(id, data){
alert("Event #"+id+" data "+data);
});
function emitLocalEvent(event/*, arg1, arg2, etc.*/) {
if(socket._callbacks) {
var args = [].slice.call(arguments, 1),
callbacks = socket._callbacks['$' + event];
if (callbacks) {
callbacks = callbacks.slice(0);
for (var i = 0, len = callbacks.length; i < len; ++i)
callbacks[i].apply(socket, args);
}
}
}
// Emitting fake events
emitLocalEvent("getSingleUpdate", 1, "Hello");
emitLocalEvent("getSingleUpdate", 2, "World!");
// You can even receive an array of events via socket.io...
socket.on("getMultipleEvents", function(eventArray) {
for(var i in eventArray)
emitLocalEvent.apply(null, eventArray[i]);
});
});
Assuming you catch events on the client side using
io.on("name",callback);
you could just call
callback();
I hope i could help! =)
I'm trying to display a list of clients in a specific room. I just want to show their username, and not their socket id.
This where I'm at:
socket.set('nickname', "Earl");
socket.join('chatroom1');
console.log('User joined chat room 1);
var roster = io.sockets.clients('chatroom1');
for ( i in roster )
{
console.log('Username: ' + roster[i]);
}
Haven't had any luck getting it to list Socket IDs or anything. Would like it to return the nicknames however.
In socket.IO 3.x
New to version 3.x is that connected is renamed to sockets and is now an ES6 Map on namespaces. On rooms sockets is an ES6 Set of client ids.
//this is an ES6 Set of all client ids in the room
const clients = io.sockets.adapter.rooms.get('Room Name');
//to get the number of clients in this room
const numClients = clients ? clients.size : 0;
//to just emit the same event to all members of a room
io.to('Room Name').emit('new event', 'Updates');
for (const clientId of clients ) {
//this is the socket of each client in the room.
const clientSocket = io.sockets.sockets.get(clientId);
//you can do whatever you need with this
clientSocket.leave('Other Room')
}
In socket.IO 1.x through 2.x
Please refer the following answer:
Get list of all clients in specific room. Replicated below with some modifications:
const clients = io.sockets.adapter.rooms['Room Name'].sockets;
//to get the number of clients in this room
const numClients = clients ? Object.keys(clients).length : 0;
//to just emit the same event to all members of a room
io.to('Room Name').emit('new event', 'Updates');
for (const clientId in clients ) {
//this is the socket of each client in the room.
const clientSocket = io.sockets.connected[clientId];
//you can do whatever you need with this
clientSocket.leave('Other Room')
}
Instead of going deep in socket/io object , You can use simple and standard way :
io.in(room_name).clients((err , clients) => {
// clients will be array of socket ids , currently available in given room
});
For more detail DO READ
Just a few things.
when you have the socket you can then set the properties like: socket.nickname = 'Earl'; later to use the save property for example in a console log:
console.log(socket.nickname);
you where missing a closing quote (') in your:
console.log('User joined chat room 1);
Im not entirely sure about your loop.
Below is the amended code should help you out a bit, also be aware the loop i am using below is asynchronous and this may effect how you handle data transfers.
socket.nickname = 'Earl';
socket.join('chatroom1');
console.log('User joined chat room 1');
var roster = io.sockets.clients('chatroom1');
roster.forEach(function(client) {
console.log('Username: ' + client.nickname);
});
to help you out more i would need to see all your code as this does not give me context.
For v4 I used this method fetchSockets()
Example :
let roomUsers=await io.in(`room-id`).fetchSockets()
see documentation here :
https://socket.io/docs/v3/migrating-from-3-x-to-4-0/#Additional-utility-methods
All the answers above and the one here socket.io get rooms which socket is currently in or here Socket.IO - how do I get a list of connected sockets/clients? were either incorrect or incomplete if you use 2.0.
In 2.0, io.sockets.manager and io.sockets.clients don't exist anymore.
Without using namespace, the following 3 parameters can all get sockets in a specific room.
socket.adapter.rooms;
io.sockets.adapter.rooms;
io.sockets.adapter.sids; // the socket.id array
With namespace (I used "cs" here), io.sockets.adapter.rooms will give a quite confusing result and the result socket.adapter.rooms gives is correct:
/* socket.adapter.rooms give: */
{
"/cs#v561bgPlss6ELZIZAAAB": {
"sockets": {
"/cs#v561bgPlss6ELZIZAAAB": true
},
"length": 1
},
"a room xxx": {"sockets": {
"/cs#v561bgPlss6ELZIZAAAB": true
},
"length": 1
}
}
/* io.sockets.adapter.rooms give: a sid without namespace*/
{
"v561bgPlss6ELZIZAAAB": {
"sockets": {
"v561bgPlss6ELZIZAAAB": true
}, "length": 1
}
}
Note: the default room is this: "Each Socket in Socket.IO is identified by a random, unguessable, unique identifier Socket#id. For your convenience, each socket automatically joins a room identified by this id."
I only tried memory adapter so far, have not tried redis-adapter.
For socket.IO v3 there's a breaking change here:
Namespace.clients() is renamed to Namespace.allSockets() and now returns a Promise.
BEFORE:
// all sockets in the "chat" namespace and in the "general" room
io.of("/chat").in("general").clients((error, clients) => {
console.log(clients); // => [Anw2LatarvGVVXEIAAAD]
});
Now (v3):
// all sockets in the "chat" namespace and in the "general" room
const ids = await io.of("/chat").in("general").allSockets();
Source
In case you're not so familiar with socket.IO, it might be good to know that instead of io.of("/chat") you can write io to use the default namespace.
For Socket v.4 correct syntax would be:
const sockets = await io.in("room1").fetchSockets();
https://socket.io/docs/v4/server-api/#namespacefetchsockets
socket.io ^ 2.0
function getRoomClients(room) {
return new Promise((resolve, reject) => {
io.of('/').in(room).clients((error, clients) => {
resolve(clients);
});
});
}
...
const clients = await getRoomClients('hello-world');
console.log(clients);
Output
[ '9L47TWua75nkL_0qAAAA',
'tVDBzLjhPRNdgkZdAAAB',
'fHjm2kxKWjh0wUAKAAAC' ]
From Socket.io v3, rooms is now a protected property of Adapter so you won't be able to access it via io.sockets.adapter.rooms.
Instead use:
const clientsInRoom = await io.in(roomName).allSockets()
OR for multiple rooms
const clientsInRooms = await io.sockets.adapter.sockets(new Set([roomName, roomName2]))
For Socket.io greater than v1.0 and node v6.0+ use the following code:
function getSockets(room) { // will return all sockets with room name
return Object.entries(io.sockets.adapter.rooms[room] === undefined ?
{} : io.sockets.adapter.rooms[room].sockets )
.filter(([id, status]) => status) // get only status = true sockets
.map(([id]) => io.sockets.connected[id])
}
If you want to emit something to them , use this :
getSockets('room name').forEach(socket => socket.emit('event name', data))
This solution is for
socket.io : "3.0.4"
socket.io-redis : "6.0.1"
import these first
const redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));
socket.on('create or join', function(room) {
log('Received request to create or join room ' + room);
//var clientsInRoom = io.sockets.adapter.rooms[room];
mapObject = io.sockets.adapter.rooms // return Map Js Object
clientsInRoom = new Set(mapObject.get(room))
var numClients = clientsInRoom ? clientsInRoom.size : 0;
log('Room ' + room + ' now has ' + numClients + ' client(s)');
https://socket.io/docs/v3/using-multiple-nodes/#The-Redis-adapter
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get
In socket.IO 4.x
const getConnectedUserIdList = async () => {
let connectedUsers = [];
let roomUsers = await io.in(`members`).fetchSockets();
roomUsers.forEach((obj) => {
connectedUsers.push(obj.request.user.id);
});
return connectedUsers;
};
I just logged all sockets in a room to the console, you can do whatever you like with them...
const socketsInRoom = io.adapter.rooms[room_name];
/*Collect all participants in room*/
for(let participant in socketsInRoom){
for(let socketId in socketsInRoom[participant]){
console.log(socketId)
}
}
you can use the adapter method on io object like
io.sockets.adapter.rooms.get("chatroom1")
this will return the list of connected clients in the particular room.
io.sockets.adapter.rooms this is a map off all clients connected to rooms with room name as keys and the connected clients are the values of the room key. map functions are applicable .
socket.io ^2.2.0
const socket = io(url)
socket.on('connection', client => {
socket.of('/').in("some_room_name").clients((err, clients) => {
console.log(clients) // an array of socket ids
})
})
Since I have found very little on how to get the rooms inside a specific namespace, here it is in case anyone is wondering :
io.of(namespaceName).adapter.rooms;
let sockets = await io
.of("/namespace")
.in(ROOM_NAME)
.allSockets();
you can get length of connected clients via
console.log(receiver.size);
You can create an array object of user collection as
var users = {};
then on server side you can add it as new user when you connect
socket.on('new-user', function (username) {
users[username] = username;
});
while displaying the users, you can loop the "users" object
On Client side
var socket = io.connect();
socket.on('connect', function () {
socket.emit('new-user', 'username');
});
This question already has answers here:
How do I get a list of connected sockets/clients with Socket.IO?
(35 answers)
Closed 3 years ago.
I have this code right now that sets the nick and room:
io.sockets.on('connection', function(client){
var Room = "";
client.on("setNickAndRoom", function(nick, fn){
client.join(nick.room);
Room = nick.room;
client.broadcast.to(Room).emit('count', "Connected:" + " " + count);
fn({msg :"Connected:" + " " + count});
});
I wanted to know how I could get how many people are connected to a specific chatroom...like Room.length
client side :
function Chat(){
this.socket = null;
this.Nickname = "";
this.Room = "";
var synched = $('#syncUp');
this.Connect = function(nick, room){
socket = io.connect('http://vybeing.com:8080');
Nickname = nick;
Room = room;
//conectarse
socket.on('connect',function (data) {
socket.emit('setNickAndRoom', {nick: nick, room: room}, function(response){
$("#connection").html("<p>" + response.msg + "</p>");
});
});
}
I found this, but it gives undefined:
count = io.rooms[Room].length;
For socket.io versions >= 1.0:
Note that rooms became actual types with a .length property in 1.4, so the 1.4.x method should be stable from now on. Barring breaking changes to that type's API, of course.
To count all clients connected to 'my_room':
1.4+:
var room = io.sockets.adapter.rooms['my_room'];
room.length;
1.3.x:
var room = io.sockets.adapter.rooms['my_room'];
Object.keys(room).length;
1.0.x to 1.2.x:
var room = io.adapter.rooms['my_room'];
Object.keys(room).length;
This is assuming you're running with the default room adapter on a single node (as opposed to a cluster). Things are more complicated if you're in a cluster.
Other related examples:
Count all clients connected to server:
var srvSockets = io.sockets.sockets;
Object.keys(srvSockets).length;
Count all clients connected to namespace '/chat':
var nspSockets = io.of('/chat').sockets;
Object.keys(nspSockets).length
If you're using version < 1,
var clients = io.sockets.clients(nick.room); // all users from room
For socket.io 1.4.6, what worked for me is specifying the namespace in addition to the room. When using the default namespace, you can just specify it as ['/']. For example, to get the number of clients connected to the room 'kitchen' in the default namespace (nsps), you would write:
var io = require('socket.io')();
io.nsps['/'].adapter.rooms['kitchen'].length
Heads up: If no one has joined a room, it hasn't been created yet, therefore io.nsps['/'].adapter.rooms['kitchen'] will return undefined. If you try to call .length on the undefined kitchen your app will crash.
In version 1.4.5
var clientNumber = io.sockets.adapter.rooms[room].length;
I wanted to get a list of users in a room.
This ended up being my solution.
I added a username property to my socket, but for completeness I changed that to "id" which is the id of the socket.
var sockets = io.in("room_name")
Object.keys(sockets.sockets).forEach((item) => {
console.log("TODO: Item:", sockets.sockets[item].id)
})
Socket.io v2.0.3
For socket.io v2.0.3, I ended up running a redis server and use socket.io-redis plugin. Then you can do:
io.of('/').adapter.clients(['room1', 'room2'], (err, clients) => {
console.log(clients); // an array containing socket ids in 'room1' and/or 'room2'
});
code is from https://github.com/socketio/socket.io-redis#redisadapterclientsroomsarray-fnfunction
kevzettler pointed me to socket.io-redis
The other answer for socket.io v2.0.3 from The Lazy Coder didn't work for me, it gave me a list of all connected clients, regardless of the room.
I'm using 1.4.6 and this did the trick:
Object.keys(io.sockets.connected).length
put this in a function and it will give you failsafe to prevent crashing:
var roomCount = io.nsps['/'].adapter.rooms[roomName];
if (!roomCount) return null;
return roomCount.length;