How can a client connect to a specific room via socketio - node.js

I have a server which emits events (something like live updates) to multiple rooms within the default namespace of a socket.io server. The server does NOT post anything to the default room. Clients connecting to the server are interested only in specific rooms and not all rooms.
How can clients connect to a specific room that they are interested in? The client connect URL has information about the server and namespace but nothing about a room. So, how should the client tell the server which room it wants to connect to?

First of all send the room name with event from client side
client side
io.emit('room', {room_name : 'test'});
on server side
io.on('connection', function(socket){
socket.on('room', function(data){
socket.join(data.room_name);
});
});

Related

Socket.io - Server listen to specific room

as mentioned in Socket.io Doc about rooms - i know that the server can move sockets to a specific room and send an event to that specific room
transfer socket to a room :
io.on('connection', (socket)=>{
socket.join('roomId');
});
emit event to sockets in that room :
io.to('roomId').emit('some event');
but i can't find a way that the server will listen to event coming only from a specific room, something like:
io.in('roomId').on('someevent',(socket)=>{....})
Socket.io has no function for this. You will have to listen for everyone on that event and check the socket's joined rooms array.

what's wrong with this server?

I'm trying to test a simple socket.io websocket server. The tool I found for testing socket.io in the command line was iocat.
The server:
var io = require('socket.io')(12345);
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('test', function(msg){
console.log('test msg: ' + msg);
io.emit('test', 'Test answer');
});
});
I started it with node socket.js command and disabled any web servers running on this machine.
This worked. The server responded ok to the connect and disconnect event but not for the 'test' message:
me#whatever:~/prj/client$ iocat --socketio ws://localhost:12345
> test
> asdfasdf
>
server response:
me#whatever:~/prj/client$ node socket.js
a user connected
user disconnected
a user connected
user disconnected
Any knows why ? I would prefer a command line tester, because I want to put my web server on https, Apache proxy and sub urls. So I want to make sure I baby-step on it and nothing else is interfering.
It appears that you have a socket.io server, but you are trying to use a webSocket command line utility to communicate with it.
A socket.io server does use a webSocket under the covers as the base level transport, but adds an additional layer on top of the webSocket and thus requires a socket.io client to complete the connection and exchange data with a socket.io server.
So:
socket.io server <==> socket.io client
webSocket server <==> webSocket client
So, you need a socket.io client to communicate with your socket.io server or you can dumb your server down to just a webSocket server using any one of several webSocket modules available for nodejs.
New part of the answer, now that the question has morphed into a question about how to use iocat properly.
A socket.io message has two parts to it, the message name and the message data. With your use of iocat, you are sending data only (with a default message name of message). It appears you need to use the -e option to set the message name to 'test' that you want to send. Or change your server to listen to the default message name of message that iocat uses. It is a bit odd that iocat doesn't let you directly specify the message name AND the message data on each transmission.

how to create channel websocket in node js server

I want to create a channel in a websocket server using node js and to send message to the subscribers.
I need a simple code to start.
Thank you for your help.
Using socket.io, you can easily create channels to differentiate subscribers to different groups. Socket.io calls the channels Rooms and you can read the documentation for details.
In essence, when clients connect to the socket, you have them join a specific room:
io.on('connection', function(socket){
socket.join('some room');
});
Whenever you emit an event (sending messages in your case), you can send it to clients in that room:
io.to('some room').emit('some event');

how to get number of socket connections using socket.io

Is there a way to get the number of connections in socket.io?
I want to display a message on my site that says "x users connected right now"
Basically if I did a socket.broadcast I want to count how many connections that would go to.
You can use
io.sockets.clients().length
Basically io.sockets.clients() returns Socket instances of all clients. If you're using rooms then you should better use
io.sockets.clients('room').length
because it returns socket instances of all clients in a particular room
Using this code Server site
var noOfUser=[];
socket.on('someEventFromClient',function(data){
//User name must unique
socket.username=data.username;
noOfUser.push(data.username);
//Now you can emit no of user to client are any where
console.log('NO of user:'+noOfUser.length);
});
socket.on('disconnect',function(){
for(var i=0;i<noOfUser;i++)
{
if(noOfUser[i]==socket.username)
{
noOfUser.splice(i,1);
}
//Now you can emit no of user to client are any where
console.log('NO of user:'+noOfUser.length);
}
});
Client side:
when user connect socket server emit this event
socket.emit('someEventFromCLient',{username:'someuniqueID'});

emits from client must be called again in server?

I have a problem with understanding how socket.io and node.js works.
I tried few examples with emitting messages and it worked. What I don't understand is emitting from clients.
I use emit in client, for example: socket.emit('custom_event');, but it doesn't work in other clients unless I add something like this in my server:
var io = require('socket.io').listen(app);
io.sockets.on('connection', function(socket) {
socket.on('custom_event', function() {
socket.emit('custom_event');
});
});
Am I doing something wrong or do I need to always add definition on server side for something that client should be emitting to other clients?
this is not possible, socketio need the server logic between your clients.
You can do:
socket.broadcast.emit
Broadcasting means sending a message to everyone else except for the socket that starts it.
io.sockets.emit
Emits to all, (inc the socket that starts the action.)
But allways from the server
Can you connect client to client via web sockets without touching the server?

Resources