I'm new to node.js and I'm working on learning how to use Socket.io to create multiple chat servers on my domain.
Here's the scenario:
you log onto the site
you pick a chat room or create a chat room
you join that individual chat room while other chat rooms are going on at the same time
Pretty standard operation on the web but I have yet to find a way to do it. Specifically, how to host it on your domain.
When creating and testing I always just use my localhost and tell the server to listen(8000) . However, how do write a script that:
A) creates a new listening port dynamically for each new chat sever?
B) how do I host it (I use Hostmonster)?
Instead of creating a separate server for each chat room, you could run all of them from the same server and just maintain a map of chat room name to the sockets involved in it.
For example,
//store a map of chat room name to sockets here
var chatRooms = {};
io.sockets.on('connection', function (socket) {
//when someone wants to join a chat room, check to see if the chat room name already exists, create it if it doesn't, and add the socket to the chat room
socket.on('joinChatRoom', function (data.message) {
var chatRoomName = data.message;
chatRooms[chatRoomName] = chatRooms[chatRoomName] || [];
chatRooms[chatRoomName].push(socket);
//set the chatRoomName into the socket so we can access it later
socket.set( "chatRoomName", chatRoomName, function() {
//when we receive a message
socket.on( "chatMessage", function(data) {
var chatMessage = data.message;
//figure out what chat room this socket belongs to
socket.get( "chatRoomName", function(err,chatRoomName) {
//iterate over the sockets in the chat room and send the message
chatRooms[chatRoomName].each(function( chatRoomSocket ) {
chatRoomSocket.emit("chatMessage", { message : chatMessage } );
});
});
});
});
});
});
Note, this code is untested and is just an idea (you should probably treat it more like pseudocode). There are a bunch of things it doesn't handle like cleanup upon disconnects, errors, etc. There are probably lots of other (and better) ways to accomplish this too but hopefully it'll give you some more ideas.
Related
So i've been making a realtime E2EE chatting application in node.js and socket.io, and I've ran into an issue
I'm using a recent socket.io version, specifically 4.2.0 and I've been attempting to find out the number of clients within a room, in order to create a check to either allow, or block access to a chat area, since it doesn't support more than 2 clients in 1 room.
My variables/includes for socket.io
var socket = require("socket.io")
var io = socket(server)
var chat = io.of('/socket').on('connection', function (socket) {
socket.on('loginchat', async function(data){
io.in(data.id).allSockets().then(result=>{
console.log(result.size) })
});
});
data.id is a randomly generated string and used to create a room server side, so my idea was to use allSockets() to get a count of sockets connected to that room, but it returns zero even with 2 clients connected.
allSockets() idea from https://stackoverflow.com/a/66065685/16140221
I can't find a working solution on stack overflow, or anything inside the docs that is an obvious method to do so. I'm not the best at socket.io since I simply picked it up to use for this project, so any help is appreciated :)
Given the example you provided, you don't seem to ever make a socket to join a room. So your room is forever empty.
According the socket.io documentation you must first listen for the connection event, then you must use join to add users to the room.
const socket = require("socket.io")
const io = socket(server)
io.on("connection", socket => {
socket.on('loginchat', async data => {
//Assuming data.id is the room identifier
const roomUsers = await io.in(data.id).allSockets();
//There, you make your socket join the room if room users are not exceeding
//your maximum
if(roomUsers.size < 2) socket.join(data.id);
});
});
var socket = require("socket.io")
var io = socket(server)
var chat = io.of('/socket').on('connection', function (socket) {
socket.on('loginchat', async function(data){
const sockets = await io.of("/socket").in(data.id).fetchSockets();
console.log(sockets.length)
if(sockets.length <= 1){
socket.room = data.id;
socket.join(data.id);
}else{
//attempting to join a room with greater than 2 users
console.log("too many")
}
});
});
https://socket.io/docs/v3/migrating-from-3-x-to-4-0/
Using the fetchSockets() and checking the length of the object, you are able to check the length or numbers of users in a room.
Working in Socket.io v4.3.1 but most likely functioning in any v4.x
I've started writing a node.js websocket solution using socket.io.
The browsers connects to the node server successfully and I get see the socket.id and all config associated with console.log(socket). I also pass a userid back with the initial connection and can see this on the server side to.
Question: I'm not sure the best way to associate a user with a connection. I can see the socket.id changes every page change and when a tab is opened up. How can I track a user and send 'a message' to all required sockets. (Could be one page or could be 3 tabs etc).
I tried to have a look at 'express-socket.io-session' but I'm unsure how to code for it and this situation.
Question: I have 'io' and 'app' variables below. Is it possible to use the 2 together? app.use(io);
Essentially I want to be able to track users (I guess by session - but unsure of how to handle different socket id's for tabs etc) and know how to reply to user or one or more sockets.
thankyou
The best way to handle the situation is rely on SocketIO's rooms. Name the room after the user's unique ID. This will support multiple connections out of the box. Then, whenever you need to communicate with a particular user, simply call the message function and pass in their id, the event, and any relevant data. You don't need to worry about explicitly leaving a room, SocketIO does that for you whenever their session times out or they close their browser tab. (We do explicitly leave a room whenever they log out though obviously)
On the server:
var express = require('express');
var socketio = require('socket.io');
var app = express();
var server = http.createServer(app);
var io = socketio(server);
io.on('connect', function (socket) {
socket.on('userConnected', socket.join); // Client sends userId
socket.on('userDisconnected', socket.leave); // Cliend sends userId
});
// Export this function to be used throughout the server
function message (userId, event, data) {
io.sockets.to(userId).emit(event, data);
}
On the client:
var socket = io('http://localhost:9000'); // Server endpoint
socket.on('connect', connectUser);
socket.on('message', function (data) {
console.log(data);
});
// Call whenever a user logs in or is already authenticated
function connectUser () {
var userId = ... // Retrieve userId somehow
if (!userId) return;
socket.emit('userConnected', userId);
}
// Call whenever a user disconnects
function disconnectUser () {
var userId = ... // Retrieve userId somehow
if (!userId) return;
socket.emit('userDisconnected', userId);
}
I would like to implement rooms for chatting in my node.js project using socket.io. I would like to create a room based on any forward slash link visited that all people who access that link are joined in that rooms.
For example:
If my website is nodeapp.com, a room can be created by going to nodeapp.com/funroom. Anyone who goes to nodeapp.com/funroom (while there is at least 1 person in the room) can globally chat.
I do not need help implementing chat, just rooms.
Thanks a lot
1) You need send room name to server:
// Client
var socket = io('/');
socket.emit('joinroom', window.location.pathname);
socket.on("new user", function(data) {
console.log("New user. Total users: ", data);
});
2) You need get room name on server and join:
// Backend
var io = require('socket.io')();
var rooms = {};
io.on('connection', function(socket) {
socket.on('joinroom', function(room) {
this.join(room);
if (typeof rooms[room] ==== "undefined") rooms[room] = {};
rooms[room].count = rooms[room].total ? rooms[room].total+1 : 1;
io.to(room).emit("new user", rooms[room].count)
});
});
I have written a similar app for my university, using the following setup:
A user browses to nodeapp.com/desiredRoom. My express server has the following route:
app.get("/:roomName", function(res, req){
res.render("student", {room:req.params.roomName});
})
This renders a template (using Jade), with the input parameter room defined by the URL. I then pass this parameter to the client as a javascript variable. The Jade template looks something like this:
html
head
script.
room="#{room}";
script(src='/clientScript.js');
body
...
On the client side, clientScript.js now knows what room to join. You can use this system to perform more advanced room requests. For example, I split up the rooms based on multiple parameters in the URL.
Trying to learn socket io and I'm playing around with a test application where:
Sockets join different rooms depending on the page url (/page1 connections join room page1, etc)
I have a separate 'dashboard' page which I want to show how many connections are from each page
I have two problems (the documentation is confusing me):
When a user disconnects, I don't know how to determine which room they were in and hence do different functions depending on the room.
Here is my attempt:
Server Code
io.on('connection', function(socket){
socket.on('joinRoom', function(room){
console.log('New connection to room: ' + room);
socket.join(room); // Clients join different rooms depending on the page.
console.log(socket.rooms); // socket.rooms here has a value
// code to increment room counter will go here.
});
socket.on('disconnect', function(){
console.log(socket.rooms); // In this line, socket.rooms is EMPTY
if (socket.rooms == room1){
// decrement room 1 counter
}
});
});
Client Code
var socket = io();
socket.on('connect', function(){
socket.emit('joinRoom', 'client');
});
My next problem, I can't seem to get the right syntax for the client to emit to a room / the server to react to messages coming from a specific room. If someone could give me an example to follow it would be extremely helpful.
Thanks for reading.
Upon join you need to "register" the room for the socket like the following:
currentRoom[socket.id] = room.newRoom;
and when they leave just call:
socket.leave(currentRoom[socket.id]);
this should do.
I'm new to node.js and socket.io. I've got a test server with the sample code from the socket.io site (http://socket.io/#how-to-use) running.
I don't know where to begin to do the following
surf to "page1" and let node know that I'm user1
surf to "page2" and let node know that I'm user2
synchronise e.g. the words user1 and user2 type somewhere on "page1" and "page2" respectively and display all words from user1 as well as user2 on both pages.
I guess I need to create some sort of routing in the "handler" function, serverside of the node.js app, but I'm unsure if that's the correct way to go about it.
Can somebody help pointing me in the right direction please?
Socket.io makes this really easy. It has namespaces and rooms. For example to create two different namespaces you would do this:
var io = require('socket.io').listen(app, {origins: '*:*', log: false});
var page1 = io.of('/page1').on('connection', function (socket) {
//you can use socket in here
});
var page2 = io.of('/page2').on('connection', function (socket) {
//you can use socket in here
});
You now have two namespaces. Inside of each you could listen for events. For example if you sent a type event:
socket.on('type', function(text){
io.sockets.emit('type', {'whatWasTyped': text]);
}
This will send a type event to all connected clients.
If you wanted to separate the namespaces even more you could create rooms. Here is how to create a room in a namespace:
var page1 = io.of('/page1').on('connection', function (socket) {
socket.on('add', function(area){
socket.join(area);
};
});
You would have to send an add event everytime you made a connection with the room you would want to join. You can then send messages to only that room then.
io.of('/users').in(area).emit('event', {'event': yourInfoHere});
You can get info about the connection by running:
socket.get('user', function(err, info){ //in here });
This was some info off the top of my head and from the socket.io wiki: https://github.com/LearnBoost/socket.io/wiki/Rooms.
I haven't tested it, let me know if you have a few more questions.