Socket.IO multiple pages for same user/socket - node.js

I'm working on a multiplayer board game on Node.js/Socket.IO. The flow is the following :
user enters his name and is added to the lobby
user select a room and is added to it, then game starts when he is joined by another user.
This part is petty easy and I've done it before. However, I now need the user to be able to join multiple game rooms at the same time. pages are dynamically generated by express, and there's no issue opening many game pages, but I'm struggling with the socket implementation.
Can I use a single socket for multiple rooms (for the same user) or do I have to create a new socket per room?
I'd like the user to always be able to chat within the lobby while in game. How do I sort that out?
Thanks

However, I now need the user to be able to join multiple game rooms at the same time. pages are dynamically generated by express, and there's no issue opening many game pages [...] Can I use a single socket for multiple rooms (for the same user) or do I have to create a new socket per room?
Pages open separately by the user do not share any context with each other. There are some hacky ways (such as a Flash LocalConnection), but you never should rely on these. Therefore, each page requires its own connection to your server.
I'd like the user to always be able to chat within the lobby while in game. How do I sort that out?
However you want. That implementation is up to you. If you are currently using the Socket.IO "rooms" feature, I suggest not using it so you have maximum flexibility in your implementation.

Related

Is it possible to store messages in a socketio chat app

Let's say I made a complete UI of a chat app. We have a panel which contains all the recently contacted. We have react on the front-end and node, express on the backend. The real time data transfer chat is carried out using socketio.
The real question is how do I store all the messages that I have with each user. Let's say I chatted with user A for a while, closed the tab and moved to user B. Then the next day I moved back to the user A, I want all my previous chats with that user to be available to me.
How is that achievable?
One way I can wrap my head around is that I use mongoDB to create a chat room with two specific user and a id. Then I store that room on mongo with all the chats stored in an array.
On the client side, I check the db if a specific chat room exists. If it does, I send all the messages in to the client side, if not it create a new chat room.
This is by far the most sensible thing I've thought.
Any guidance as to what other techniques I can use?
Much appreciated.

Is it possible to check if anyone has a certain browser link open and if not execute a function?

I am making a web app that involves the creation of several rooms (users create them). These rooms are then stored on an array on the server. Using react router each of these rooms are given their own unique url /roomCodeGoesHere. I want to periodically check the rooms to see if anyone is in it(I guess in this case has the browser open?) and if there is no one in it, to have the room be deleted from the array.
Is there any way I can go about doing this? (the only ways I can think of seem hacky and are probably not good practice)
You can monitor the user count within your server array (via put request or websocket on mount/unmount), and if the room empties conditionally delete it within the controller. A room should by default start with 1 occupancy since it needs a creator. If you don't want to delete the room right away, then do the same thing but create a setTimeout that conditionally clears itself unless someone new enters.
This chat app I made deletes a room at 0 users using the above method:
http://astral-chat-app.herokuapp.com/

NodeJS and Socket.io creating room with two users

I'm doing chat app and I want to do it real time. Chat will be for now between two users. When user access to chat it finds all messages between them and displays messages. I think I need to create room with two users and then store room id in database. But I'm new to socket.io and I need advice how to do it.
Try to take some already pretty wide used chat, like Slack, as example. Usually you need pretty same set of things, workplaces, channels, private messages (like room but for two users only), and have users sending text messages with some formatting or images or just any files. Just take it easy, plan and make it step by step.
Also take note, that for both parties have their chat view updated with new messages you need not only save message one user send to db, but also broadcast that message to everyone, involved in conversation.

Socket.io: Live Chat Design, better way to initialize list of rooms and people?

I am trying to create a meeting app using socket and node. So far, following this tutorial (https://scotch.io/tutorials/a-realtime-room-chat-app-using-node-webkit-socket-io-and-mean) I see that they are making the list of available users to chat using Mongo.
My meeting app consists of available rooms and people (so there are list of rooms available and list of people that is inside a room, or not anywhere in the room, you cannot chat with people that is not in a room yet). Is it better if I make the list of rooms and people in just a variable and not on database (and is it possible that new people joining have access to that variable)? And if indeed using variable, how many concurrent user connection that can connect at same time, like should I allow only 1000 users connect at same time if I'm using variable method?
I suggest you continue to use MongoDB. You would not actually store the connections though. You would want to store the room IDs (along with their name, description, etc.) and user IDs. In the Users collection, you would also store their name, bio, online/offline status, etc. Either you build a new document every time a new user connects, or you implement some sort of authentication system so users can login in from multiple sources.
If you use MongoDB, you really don't have to worry about having too many people online. It can handle whatever you give it.

Node.js - how to use one connection with socket.io for several rooms

I need to do a chat, therefore I guess to using node + socket.io. Except that, one person may be on several room in same time. Although we can use io.of('/room1')...
This forces, browser side, to make several connections to several rooms.
Have you got a best suggestion or idea ?
Thank you in advance.
You can think of having a "default" room, which will be helpful to maintain presence i.e status of the user whether he is online or offline. This will contain a big list of all users who are online in your application
For each chat that happens between users, better to have dynamic rooms created for each conversation.

Resources