NodeJS and Socket.io creating room with two users - node.js

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.

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.

sort buddy list in real time 1:1 chat with pubnub react sdk

I am trying to design a real time in-app chat for social application using PubNub. I found that the best architecture to do so for one to one chat with PubNub is detailed in this article http://pubnub.github.io/pubnub-design-patterns/2015/03/05/Inbound-Channel-Pattern.html
now my next problem is I have to display the list of users in the chatting window, how can I sort this list with the users who have sent messages latest at the top and the ones who have not interacted for a long time in the bottom. if I start fetching message from the inbound channel, I will have to always traverse inbound channel to the beginning every time a user has logged in, this will be a resource expensive call and also is not feasible if we have large user base and heavy message volumes.
I will also be using PAM to control authorization of users to read / write on channels.
That is indeed a great blog entry!
If you are in hybrid mode, so you do use a replicate channel for History feeding anyway, then I would use that same channel and intercept it the content with a function and simply store in the channel Object the list of latest visitors ordered, by the latest showing first, you can even add any extra info you would like to it. Then, any time a user can access the Object values from a REST function to PubNub, so as to retrieve the "hybrid channel" associated Object values (stored earlier) and send that list that is always updated to the Chat user. This has an advantage: if you do not want to retrieve messages until a user taped on one of the contacts in the contact list to avoid pre-loading, then you would load no messages for any channel, except maybe the first user, but then its always less to load from History then all messages from all of the channels, and its always available, before fetched, so fastest.

Socket io rooms (MEAN stack)

I am building a buy and sell website that is built on MEAN stack. Im planning to use socket.io. So here's how my website will work
User will register and login
They can post an item either buy/sell
Other users can offer to the post (i'll use socket.io on this so that the user who posted the item will have a notification)
The poster will have an option to view the current offers, then they will choose who'll they accept.
Once they accept the offer, both the Poster and the User who offered will have a communication both of them (Chat room)
So when the poster accepted the offer of the customer, it will open a chat for them to communicate. So here's my question, i will use socket io rooms. Once the poster accepts the offer, both of them will join the room (so i'll generate a room for them and automatically join them) then this room will be saved to the MongoDB, then every message they'll send, it will be saved to the database. So that they can see the history of their message even though they logout.
Is this the best method?
Create a room for both of them (saved to DB)
Saved to database everytime they send a message to each other
Query the database when they login for list of the rooms the user has joined so that they could see the messages they have
That would probably be the best way to do it. Here is a similar question is there a good way to save socket.io message history it shows how to save the message history.
You do not have to save the messages, but if you would like to be able to see messages later, you should save every message to a database. If you do not save every message to a database, when you reload the page all previous messages will disappear.
If you want a free database, mongoLab will host small mongoose databases for free. Here is a good tutorial showing how to use mongoLab, it does not use socket.io, but it will probably be very easy to implement it.
Hope this helps! Feel free to ask any questions below.

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.

Socket.IO multiple pages for same user/socket

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.

Resources