Anthony Toma • February 16, 2022 • Comments Off on Build an email in chat room (/:roomId/message) [POST consult]
One thing to see let me reveal userIds, type is coming from req.body while userId this is certainly becoming aliased as chatInitiatorId is coming from req thanks to the decode middleware.
In the event that you recall, we connected app.use(“/room”, decode, chatRoomRouter); inside our server/index.js file. Meaning this path /room/initiate are authenticated. Thus const < userId:>= req; could be the id associated with the existing user signed in.
We simply contact all of our initiateChat process from ChatRoomModel and pass they allUserIds, means, chatInitiator . Whatever lead comes we simply go it on consumer.
Nevertheless before we build a message we have to write a model in regards to our chatmessages . So let’s do that initial. In your types folder make a document also known as ChatMessage.js and create the next information to it:
I understand this can be many material, but simply carry with me. Let’s merely create the controller for all the path that brings this message.
The path defined in our routes/chatRoom.js API (‘/:roomId/message’, chatRoom.postMessage) let us check-out its controller in controllers/chatRoom.js and determine they:
For your route .get(‘/:roomId’, chatRoom.getConversationByRoomId) in routes/chatRoom.js open its operator in file controllers/chatRoom.js and create the next contents into the talk place:
And eventually, visit your ChatMessage unit in models/ChatMessage.js and compose a new static method also known as getConversationByRoomId :
As soon as the other individual is logged in and they see a conversation for a room id, we need to mark that talk as read from their side.
All we’re starting here is basic verifying in the event that room prevails or not. In the event it do, we continue furthermore. We take in the req.user.id as currentLoggedUser and pass they on the following features:
A potential incorporate case is the fact that the user might possibly not have look at the final 15 information whenever they create a certain room conversation. They need to be marked as study. Therefore we’re utilising the this.updateMany work by mongoose.
This says I want to get a hold of all of the content content in chatmessages range in which chatRoomId fits and readByRecipients collection will not. The userId that I am moving for this function is actually currentUserOnlineId .
Next we want to inform mongoose to not www daddyhunt co simply modify the initial record it finds, additionally to upgrade most of the data where the situation matches. So doing this:
Get in touch with me personally on twitter together with your feedback a€“ I would personally like to notice for those who have any ideas for improvements: twitter/adeelibr
In the event that you appreciated for this article, be sure to perform supply the github repository a celebrity and subscribe my personal youtube station.
Learn to code for free. freeCodeCamp’s available provider course have aided more than 40,000 someone become employment as developers. Start out
All of our purpose: to help people learn to code free of charge. We accomplish this by generating a large number of video clips, reports, and entertaining programming lessons – all freely available for the public. We have countless freeCodeCamp study teams across the world.
In your root folder establish a folder also known as config . Inside that folder establish a file called list.js and create listed here content:
Right here our company is advising it that firstName are of kind string. If the user forgets to incorporate this benefits while hitting the API, or if perhaps the nature is certainly not string, it is going to place one.
We become the userId from your req.params . Any time you remember through the video clip previously, req.params could be the /: described in our channels point.
We will take that individual id plus the client id (an individual’s own distinctive outlet id that creates when they create a connection with your feel).
We’re with the make-validation collection right here to validate an individual’s demand. When it comes down to initiate API, we count on the consumer to send several consumers and establish whatever the chat-room this is certainly are produced.