how to create channel websocket in node js server - node.js

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');

Related

how do i send a message to a specific client in Socket io?

I'm using nodeJs ,express, mongodb & socket.io for real time notifications and messaging. I have successfully broadcasted notification to all clients but i'm having trouble sending a notification to a specific client.
I want to use clients object Id, is it possible??
To send an event to a specific socket use io.to with the socket.id of the receiver.
io.on('connection', socket => {
io.to(socket.id).emit('private', 'Just for you bud');
});

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.

socket.io: why doesn't this broadcast statement work?

I'm trying to use broadcast statement to send messages to specific user.However, when I'm starting with self-sending messages using the following statement, it doesn't work. The client side cannot receive the message. So what's wrong with this statement?
io.on('connection', function(socket) {
socket.on('message', function(msg) {
socket.broadcast.to(socket.id).emit('message', msg);
});
});
(purpose of this code: the client side send a piece of message named 'message', the server receive the message and send back to the same client)
socket.broadcast.to broadcasts to all sockets in the given room, except to the socket on which it was called while io.sockets.in broadcasts to all sockets in the given room. (cited from Daniel Baulig's answer to another question:)Socket.io rooms difference between broadcast.to and sockets.in

How can a client connect to a specific room via socketio

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);
});
});

Socket.IO messaging to multiple rooms

I'm using Socket.IO in my Node Express app, and using the methods described in this excellent post to relate my socket connections and sessions. In a comment the author describes a way to send messages to a particular user (session) like this:
sio.on('connection', function (socket) {
// do all the session stuff
socket.join(socket.handshake.sessionID);
// socket.io will leave the room upon disconnect
});
app.get('/', function (req, res) {
sio.sockets.in(req.sessionID).send('Man, good to see you back!');
});
Seems like a good idea. However, in my app I will often by sending messages to multiple users at once. I'm wondering about the best way to do this in Socket.IO - essentially I need to send messages to multiple rooms with the best performance possible. Any suggestions?
Two options: use socket.io channels or socket.io namespaces. Both are documented on the socket.io website, but in short:
Using channels:
// all on the server
// on connect or message received
socket.join("channel-name");
socket.broadcast.to("channel-name").emit("message to all other users in channel");
// OR independently
io.sockets.in("channel-name").emit("message to all users in channel");
Using namespaces:
// on the client connect to namespace
io.connect("/chat/channel-name")
// on the server receive connections to namespace as normal
// broadcast to namespace
io.of("/chat/channel-name").emit("message to all users in namespace")
Because socket.io is smart enough to not actually open a second socket for additional namespaces, both methods should be comparable in efficiency.

Resources