I'm trying to count the total number of users in a specific room and broadcast it to all the people in that room.
Here is what I have but I get an error:
var clients = io.sockets.clients(cc.lowerCase(data.roomname)).length;
io.sockets.in(cc.lowerCase(data.roomname)).emit('updatetotal', { total: clients });
ERROR:
TypeError: Object #<Namespace> has no method 'clients'
Thanks.
Since socket.io 1.0 its API was significantly changed so the old code might not work.
To get the number of clients in a room you can use this function:
var getUsersInRoomNumber = function(roomName, namespace) {
if (!namespace) namespace = '/';
var room = io.nsps[namespace].adapter.rooms[roomName];
if (!room) return null;
var num = 0;
for (var i in room) num++;
return num;
}
or more laconically:
var getUsersInRoomNumber = function(roomName, namespace) {
if (!namespace) namespace = '/';
var room = io.nsps[namespace].adapter.rooms[roomName];
if (!room) return null;
return Object.keys(room).length;
}
This function takes two agruments:
roomName
namespace (optional) default = '/'
To send message to users of this room only use .to method:
io.to(yourRoomName).emit('updatetotal', { total: getUsersInRoomNumber(yourRoomName) });
to get total users in a room
io.sockets.adapter.rooms.get(roomName).size
Related
I'm using my own rooms implementation in Socket.io (got my own Room/Player classes), since I need to perform several filters the room. For now, I save all sockets inside "players" property in a Room, and implemented my own "emit" to the room, the loops players and emits to their sockets.
Is it considerably slower to the traditional broadcast.to('room')? or it basically does what I did to my own room implementation? I'm aiming on having thousands of rooms with 2-4 players in each...
Thanks :)
As you can see by looking at the code for the socket.io adapter .broadcast() on GitHub, all socket.io is doing is looping over a list of sockets and sending a packet to each one (see code below).
So, if your code is doing something similar, then performance would likely be something similar.
Where you might notice a feature difference is if you are using a custom adapter such as the redis adapter that is used with clustering, then the logic of broadcasting to users connected to different servers would be handled for you by the built-in adapter, but may be something you'd have to implement yourself if doing your own broadcast.
Here's the socket.io-adapter version of .broadcast():
Adapter.prototype.broadcast = function(packet, opts){
var rooms = opts.rooms || [];
var except = opts.except || [];
var flags = opts.flags || {};
var packetOpts = {
preEncoded: true,
volatile: flags.volatile,
compress: flags.compress
};
var ids = {};
var self = this;
var socket;
packet.nsp = this.nsp.name;
this.encoder.encode(packet, function(encodedPackets) {
if (rooms.length) {
for (var i = 0; i < rooms.length; i++) {
var room = self.rooms[rooms[i]];
if (!room) continue;
var sockets = room.sockets;
for (var id in sockets) {
if (sockets.hasOwnProperty(id)) {
if (ids[id] || ~except.indexOf(id)) continue;
socket = self.nsp.connected[id];
if (socket) {
socket.packet(encodedPackets, packetOpts);
ids[id] = true;
}
}
}
}
} else {
for (var id in self.sids) {
if (self.sids.hasOwnProperty(id)) {
if (~except.indexOf(id)) continue;
socket = self.nsp.connected[id];
if (socket) socket.packet(encodedPackets, packetOpts);
}
}
}
});
};
I have this app in socket.io using node.js server and I want to update the points for every one connected in the app. I keep a list of connected users by socket.id and secret in the clients array.
And I tried like this but the points won't update. Can anyone tell me what's wrong?
function updatePoints(){
points = [];
pool.getConnection(function(err, connection) {
connection.query("SELECT * FROM `users`", function(error, rows) {
for (var i = 0; i < rows.length; i++) {
points.push({"secret" : rows[i].secret, "points" : rows[i].pontos});
}
for (var l = 0; l < points.length; l++) {
for (var p = 0; p < clients.length; p++) {
if(points[l].secret == clients[p].secret){
var socketID = clients[p].socket;
var pontos = points[l].points;
io.sockets.connected[socketID].emit('update points', pontos);
}
}
}
});
});
}
Socket.io version: 1.3.7
How about make a client object theb assigning a socket and a point property then push it in a the clients array?
socketServer.on('connection', function(socket){
//make your query here
...
//make a client object
var client = {point: row.point, socket: socket};
clients[row.secret] = client;
});
then you can retrieve the client by:
//query...
....
clients[row.secret].socket.emit('something', function);
edit:
if you want to broadcast to connected clients then you can:
socketServer.emit('something', function);
if you want to broad to everyone except the one who emitted the broadcast. then you can:
clients[row.secret].broadcast.emit('something', function;
Replace the
io.sockets.connected[socketID].emit('update points', pontos);
with this
socket.broadcast.to(socketID).emit('update points', pontos);
I am trying to adapt a piece of code that was coded with socket.io 0.9 that returns a list of clients in a specific room and list of rooms(typical chat-room example)
Users in room
var usersInRoom = io.sockets.clients(room);
List of rooms
socket.on('rooms', function() {
var tmp = io.sockets.manager.rooms;
socket.emit('rooms', tmp);
});
tmp looks like this
{
0: Array[1],
1: /Lobby: Array[1]
}
So I can show the list in the client with this javascript run on the browser.
socket.on('rooms', function(rooms) {
$('#room-list').empty();
debugger;
for(var room in rooms) {
room = room.substring(1, room.length);
if (room != '') {
$('#room-list').append(divEscapedContentElement(room));
}
}
$('#room-list div').click(function() {
chatApp.processCommand('/join ' + $(this).text());
$('#send-message').focus();
});
});
But for version >1.x I just found the clients/rooms changed.
Following some links I found here, I could manage to get a list of rooms by doing this:
socket.on('rooms', function(){
var tmp = socket.rooms;
socket.emit('rooms', tmp);
});
The problem here is that socket.rooms returns
{
0: "RandomString",
1: "Lobby",
length: 2
}
And I just need to pass the 'Lobby' room. I don't know from where the random string come from.
EDIT
Through some debugging I discovered, the randomstring is the socket.id ... Is it normal this behavior? Returning the room and the socket.id together?
Updated
I finally got some results
Users in room
var usersInRoom = getUsersByRoom('/', room);
function getUsersByRoom(nsp, room) {
var users = []
for (var id in io.of(nsp).adapter.rooms[room]) {
users.push(io.of(nsp).adapter.nsp.connected[id]);
};
return users;
};
List of rooms
function getRooms(io){
var allRooms = io.sockets.adapter.rooms;
var allClients = io.engine.clients;
var result = [];
for(var room in allRooms){
// check the value is not a 'client-socket-id' but a room's name
if(!allClients.hasOwnProperty(room)){
result.push(room);
}
}
return result;
}
Better and more straightforward ways to achieve the results?
These are the links I checked:
How to get room's clients list in socket.io 1.0
socket.io get rooms which socket is currently in
It seems there is no better approach, so I will use my own answer. In case any of you has a better solution, I would update the accepted one.
Users in room
var usersInRoom = getUsersByRoom('/', room);
function getUsersByRoom(nsp, room) {
var users = []
for (var id in io.of(nsp).adapter.rooms[room]) {
users.push(io.of(nsp).adapter.nsp.connected[id]);
};
return users;
};
List of rooms
function getRooms(io){
var allRooms = io.sockets.adapter.rooms;
var allClients = io.engine.clients;
var result = [];
for(var room in allRooms){
// check the value is not a 'client-socket-id' but a room's name
if(!allClients.hasOwnProperty(room)){
result.push(room);
}
}
return result;
}
I have code like that
var subscribeNewMessages = require("redis").createClient(config.redis.port, config.redis.host);
subscribeNewMessages.subscribe('new-messages');
io.of('/new-messages').on('connection', function (client) {
subscribeNewMessages.on("message", function(channel, message) {
var obj = JSON.parse(message);
if (client.userId == obj.toUserId || client.guestId == obj.toUserId) {
client.send(message);
}
obj = null;
});
})
And how can I optimize it? Because this code parses string json for each new messages.
Also when I try to publish to redis chanel I need to JSON.stringify
redis1.publish(channelPrefix, JSON.stringify(clientData));
There isn't going to be a way to avoid JSON.parse()/JSON.stringify() as long as you're storing js objects. You could use a different serialization format like msgpack, but you're still calling functions to serialize/unserialize your data (also JSON.parse()/JSON.stringify() are already pretty hard to beat performance-wise in node).
I think the only real performance adjustment you could make with the code you've provided is to only parse the JSON once for all clients instead of for each client. Example:
var subscribeNewMessages = require('redis').createClient(config.redis.port, config.redis.host);
subscribeNewMessages.subscribe('new-messages');
var nsNewMsgs = io.of('/new-messages');
subscribeNewMessages.on('message', function(channel, message) {
var obj = JSON.parse(message),
clients = nsNewMsgs.connected,
ids = Object.keys(clients);
for (var i = 0, len = ids.length, client; i < len; ++i) {
client = clients[ids[i]];
if (client.userId == obj.toUserId || client.guestId == obj.toUserId)
client.send(message);
}
});
Depending on your application, you might even be able to avoid the for-loop entirely if you can store the socket.id values in your published messages, then you can simply look up clients[obj.userSockId] and clients[obj.guestSockId] because the connected is keyed on socket.id.
How would I go about returning a list of rooms depending on how many people are in each one?
I'm using socket.userID for each connection which I'm sure could be used to calculate this.
This may be done with the following two functions (the first one is taken from this topic):
var getRoomUserNum = function(roomName, namespace) {
if (!namespace) namespace = '/';
var room = io.nsps[namespace].adapter.rooms[roomName];
if (!room) return null;
return Object.keys(room).length;
}
var getRoomsSortedByUserNum = function(namespace) {
if (!namespace) namespace = '/';
var roomsUsersNum = {};
var roomNames = [];
for (var roomName in io.nsps[namespace].adapter.rooms) {
roomsUsersNum[roomName] = getRoomUserNum(roomName, namespace);
roomNames.push(roomName);
}
roomNames.sort(function(a, b) {
return roomsUsersNum[a] < roomsUsersNum[b] ? 1 : -1;
});
return roomNames;
}
Then you just call getRoomsSortedByUserNum(); optionally passing your namespace as an argument (which is / by default), and it returns an array of room names sorted by number of users in each one.
But keep in mind that when each socket is connected, it's own room is automatically created. So if you have 20 sockets and 3 manually created rooms, you actually have 23 rooms.