Send Notification to dashbord app When New order is created in Database using Nodejs - node.js

Commerce web app. i want to send notification to be showed in dashboard when new order is created.how can i achieve this using MEAN stack (mongodb express angular and node). can i do this using socket.io. what is procedure to be followed.

You are correct in looking towards socket.io. Socket.io will allow you to do this very easily.
Frankly, you should be able to read the tutorial linked below and know what you need to do, as notifications are probably the easiest thing you could do with socket.io.
http://socket.io/get-started/chat/
Basically what you'd want to do is have your clients listen for an event from socket. You'll emit the event from socket when a new order is created.

Related

What is the best way to implement real-time notifications in a MEAN application?

I want to make an app which lets users comment and send messages. However, the notifications for these events will have to come instantly, just like any other social-media or chat application. This is what I'm thinking of:
Web-frontend: Angular, mobile: Ioinc with Angular
Backend: Node, Mongo
Now, this is how I was thinking I'd implement real-time notification.
There's a constant socket connection between the frontend (web & mobile-app) and the backend.
Whenever a message arrives, targeted to a specific user, I'll use some kind of a Mongo-hook to send the notification to the frontend via the socket connection.
Now, the confusion with this approach is:
Would millions of socket connections work at scale, at all? If not, what is the way to implement this pub-sub kind of system? I need to do it from scratch, not using Firebase.
What if a user is offline when he receives the message in the backend? If the socket is not on, how would he get the message? Is there a way to do it using Kafka? Please explain if you have some ideas on this.
Is this the correct approach? If not, can you suggest what would be appropriate?
Would millions of socket connections work at scale, at all? If not, what is the way to implement this pub-sub kind of system? I need to do it from scratch, not using Firebase.
Yes, it can work at scale just you have to made an architecture like that. You might find this useful
Scalable architecture for socket.io
https://socket.io/docs/v3/using-multiple-nodes/
What if a user is offline when he receives the message in the backend? If the socket is not on, how would he get the message?
If he the socket is not on or user is offline, then client Socket will be disconnected. At this point, notification will not be received and whenever the user comes online you'll have make an API call to get the notifications and connect again to the socket for further operations.
Is there a way to do it using Kafka?
Yes, you can also do it with Kafka. You'll need Consumer API(Subscriber) and Producer API(Publisher)
https://kafka.apache.org/documentation/#api
https://www.npmjs.com/package/kafka-node
Sending Apache Kafka data on web page
What do you use Apache Kafka for?
Real time notification with Kafka and NodeJS

Socket.io handle online/offline status

I've a real time chat application i've implemented it using socket.io, nodejs and angularJS but i'm having issues while handling the online/offline status. I'm not clear on how to know when the user is online or offline. What i thought is hitting a api in every 3 seconds and update the online/offline status in the database but i don't feel it's a good way so is there any better way to handle it?
Thanks in advance!
Here is the link to my backend code.
DesktopChat-Backend
You don't need to hit every 3 seconds , socket.io do this automatically, so you have just to handle it inside on. ('connection') and on.('disconnect'), so you get the user connected or disconnected then you broadcast the information to the clients, and update their status.
Look at this example :
Showing online users using Nodejs and Socket.io and angular js
One-to-One-Chat-using-Node.js-Socket-IO-Library
you can make map, where your userId will be associated with all socket.io client ids for this user. Then you can just look into this map, if specific user has some open connections.

How to implement Laravel, node.js, socket.io and redis for creating realtime chat/notifications with database

I spend already 3 days for reading, watching tutorials about WebSockets, socket.io, node.js and so on.
Basically, I'm a Laravel developer and have just a basic idea about all the rest components.
With regret, after these 3 days I don't have in mind all logic step-by-step of implementing this architecture. I will try to explain what did I understand and you please correct me.
So :
WebSockets - is a bidirectional continuous connection between client and server. It uses another port, and basically it is not a HTTP/S connection.
For making this kind of app like I said, we need one more server, and idk why, but this is Node.js. At this Node.js server we should install socket.io (server-side package) and Redis.
Then, we need to add client-side socket.io (via CDN probably).
At Node.js server we are creating a server.js file where require all modules that we need, like socket.io and Redis. We open a connection for a specific non-used port (as 6001). Then we run this node server.
At front-end we are subscribe-ing to this channel and define method for emitting and listening to the server.
Example :
User1 is connecting to a specific route. User2 as well. User1 type a message for User2, when press Submit, message from User1 is sent to Node.js server, where it is sent in Redis(yes? if yes - Why?), and then Node.js is listening for what to do in this case, and send this message to specific user, or with broadcast to all users except the publisher.
Oh, it's even hard to explain that, too much steps and tehcnologies used.
Can please someone correct my logic? I really want to understand all that process and logic of using this components. Or please, give me some useful articles and videos, may be I didn't saw them. Thanks!
I suggest you read the official docs on how to build chat. Basically what you will have in the end is 2 servers, 1 for your Laravel app and the other for chat (Socket.io) . The key to this is using broadcasters and listening for events on both sides, frontend and backend.
Events are broadcast over "channels", which may be specified as public or private. Any visitor to your application may subscribe to a public channel without any authentication or authorization; however, in order to subscribe to a private channel, a user must be authenticated and authorized to listen on that channel.

Socket io private messages using expressjs nodejs mongoose

I'm struggling with socket.io to make a chat app with the users that are logged in in my app. I followed the beginner course and I could make a private chat app, but the users aren't the ones that are in my database or they have to create a username to connect to the chat app.
In my search I found this one -
Realtime app with Vue, Laravel, Socket.io and Redis (He's making a similar app using Laravel)
I want the exact same app but using nodejs mongoose express.
Can you help me with some references or tutorials?
Here are some resources that might -
Make A Real-Time Chat Room using Node Webkit, Socket.io, and MEAN
Simple Chat Application using NodeJS and Socket.IO
Please start with any of these and be more specific with what extra feature you're trying to implement. (code would be very helpful)
Couldn't understand what do you mean by the users not in database bit.

Switching from stored messages to real time chatting in node and express

I am new to server-side development. I'm trying to learn by doing so I'm building an application with express on the server, mongodb as my database and angularjs with twitter bootstrap on the client-side.
I dont know if this is the most practical way but when thinking about how to implement messaging between users I thought of a mongodb model called Conversation with an id and an array of the ids of every user in the conversation and another array of strings that correspond to messages. And then add this model to my REST API.
But lets say all/some of the users in the conversation are online, why not benefit from socket.io. So how can i switch from this to real time chat? Does the interaction with mongodb occure exactly as explained and socket.io just notifies every online user that an interaction has occured? If yes, how? Or is it something else?
socket.io can send real time events to connected sockets, you can use a database for storing messages that are failed to deliver and for offline users.
also, you might want to use something like Redis for this as it has channels with subscribe and publish capabilities.

Resources