Socket io rooms (MEAN stack) - node.js

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.

Related

Real-time notification feature in MERN stack

I am trying to build a web app where users can receive notifications if their posts get likes. This functionality is actually working but the notification component renders only when the page reloads. But I want it to re-render the notification component as soon as there is a change that occurs with it. I am trying to make this work with SocketIO both in client and server but it feels like there should be a way easier way to solve this issue. How?
One way to achieve this is to use server-sent events (SSE), which can be seen as a uni-directional
and simplified version of WebSocket (only server can send data to clients). You might need route param or query string so that users can connect to different channels.
If it doesn't have to be so real-time (i.e. receiving notifications 1 minute after somebody liked the post is okay), you could also try (long)polling.
However, since post-like table may contain a huge amount of data, constantly querying it is expected to result in bad performance on the server. Also, adding an extra column to mark if the user has been notified about a specific like is trivial.
A possible workaround might be to create a new table, say pending-like-notification, which is used to stored all pending like notifications. Whenever a post is liked, we insert a new record into this table.
Therefore, when a user tries to query if any of his posts is liked recently, in the backend we lookup this table instead of the post-like table, and delete the selected rows after the user has been notified so that the amount of data in this table can stay low (hopefully).
All in all, in this scenario, I think SSE and WebSocket will be better choices.

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.

Private chat app which saves contacts/message history with MongoDB Nodejs Socket.io and Redux

I'm quite new with sockets so I came here to ask for some good practice ideas.
To understand better what I want to achieve here is my idea: I made a product creation page in react which has a seller and an option "Send message to the seller". After a buyer clicks to this button I want to make a private chat between the two user.
I want to save this socket connection and all of the messages between the users for both of them in my MongoDB database. So whenever a user simply leaves the site all of the recieved contacts/messages are saved and when he rejoins can continue the chat with the pre-loaded chat history.
So I thought about creating a messages collection and store socket details in document like this:
_id:ObjectId("123")
productId:1
sender:id1
reciever:id2
messages=[{1:"Hey},
{2:"Nice to meet you"} ...]
And here comes the redux part: When a user is logging in, I want to fetch all of the previous contacts and messages so he can continue chatting with them. But it would be time waisting to fetch the message history every single time, so I thought about using a redux store to get the contacts and history from database and store there. Like this
recievedmessages=[{_id:ObjectId("123")
productId:1
sender:id1
reciever:id2
messages=[{1:"Hey},{2:"Nice to meet you"} ...]},
{othermessage1 ...},
{othermessage2 ...},
]
sentMessages=[{sentmessag1, sentmessage2, sentmessage3}]
And when a buyer sends a new message to that user I update my redux store for the buyer, when the seller recives the message from the socket update for him too and lastly my chat history in my mongodb messages field for that specific document. --> I am not sure at this part to have to manage it
Is this a good practice or how should I make this to be effiecent and store data? Thanks in advance.

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.

How to use redis for notifications in NodeJS/MongoDB?

I have running on NodeJs/MongoDB and Android/HTML client where people are able to follow posts, I store their ids in an array of followers in the post's document.
I want to implement a notification feature when a new comment comes I want to notify all the followers.
So, I have looked across the web but I couldn't get one tutorial where I understood exactly how things work.
I have seen many recommend the Redis Pub/Sub, but from what I have read Redis is a in-memory database, so I was thinking, I could create a channel in Redis with the post's Id, when the user follows that post I would subscribe him to the channel, then when a new comment comes i just publish to the channel. Is this how it works?? If so, then how about when the user goes off then comes back in later, And how about when there are many posts, won't Redis clear the channel then I wouldn't be able to send the notification to all the followers?
I think I'm understanding it all wrong,I'd appreciate your help. How exactly can I be able to send a notification to all followers?

Resources