Real-time notification feature in MERN stack - node.js

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.

Related

Best way to implement Notification system using Nodejs and Postgresql Database

currently i'am developing a react app where user can post and comment and like a post.
so i did everything but there is one more thing that i want to do which is getting notification after user
Post or maybe comment and so on..
so let say i have a table called 'posts' in the database (PostgreSQL) and when user called "A" create post ,that post inserted into the database and then i want to receive a notification which Said that the user "A" created a post with the ID of that post and i think the main idea here is to watch the table of posts and whenever a new raw inserted a notification fire.
i searched many sources and find that i can use Pusher ,web-socket but to be honest i can't make a decision without figuring out the best way to achieve that.
So How i can get notified hence a new row inserted into the table "posts" ?
Thank you in advance.
Postgres has LISTEN/NOTIFY that you could use in conjunction with an on-insert trigger on the posts table.
Some process can then be listening for those notifications and figure out whom to send the notification to (via whichever transport you choose).
However, I think you might be better off adding a concrete table for notifications, so the user can see notifications that occurred when they weren't connected to the notification transport.

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/

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.

Using socket.io to send data to a specific view/id

I have a web application using NodeJS, Express, and MongoDB. In my application, I have a view, that can be seen by anyone who accesses the application. That view is rendered with a different image, depending on which a user selects to view (they do not need to be logged in) ie the view is mapView/mapId.
Now, I want something similar to notifications to occur in realtime for those that are on that page. When a specific event happens from an external source, I want to display a popup on the view to which the event belongs to. So the event may only belong to one mapView/mapId and not another mapView with a different ID. All users on the same mapView/mapId should see the notification. Remember, these are general users that do not need to be logged in.
I am researching into Socket.io because I know it is for making realtime applications. But I am wondering if this is even the right way to go. How will I send data to the correct mapView/mapId?
Check out what your server can do with rooms
The idea is that each of your connections, from a particular view, is joined to a room. Then you use socket.io from the server to send a message only to that room. And only those sockets will get the message.

What to do when entities keeps changing in Azure Search?

I'm working on a chat-like application and would like to provide search capability for it. I was thinking of using Azure search. Once possibility is to consider each chat room as a single entity in the Azure search so that each entity represents a single chat room. The challenge I encounter is because each chat room keeps receiving new messages, the index for a chat room keeps changing. I can use a queue mechanism to queue up the changes and update my entities in the Azure Search but I don't know whether it is a good and scalable solution. Obviously I need to increase the number of replicas to keep up with the changes. Any recommendation? Is Azure search a suitable tool for such a scenario?
H.Z.,
We do have customers that use us for Chat / Instant Message based applications, so we might be viable for your needs. Before I answer your questions, can you tell me what types of things you would want to be able to search?
Is it merely to search the chat rooms or is it to search both chat rooms as well as the messages within these chat rooms? If it is just to search the chat rooms, you might want to consider going your suggested approach and then store the chat messages in a lower cost store (such as Azure Storage) and then simply pull in the message text as needed.
If it is the latter, then the next question would be whether you want to be able to do a search that spans both chat rooms and the messages within them which would indicate that it makes a lot of sense to have them within a single index, although whether you put all messages in a single document or have one document per message we should drill into a bit more. The main issue I would think of with having all messages in one index is in the case where a user wants to drill into (or filter) specific messages within a chat room. I am thinking this might be a little harder then if you split messages into separate documents.
Another option might be to have 2 indexes. One that has the chat rooms and then another that has the individual messages within them.
In any case, I hope that helps as a start and based on your responses hopefully I can help you drill into the details more.
Liam

Resources