One thing to see is userIds, type comes from req.body while userId that’s are aliased as chatInitiatorId is coming from req by way of our decode middleware.
If you keep in mind, we affixed app.use(“/room”, decode, chatRoomRouter); within server/index.js document. Meaning this path /room/initiate try authenticated. Thus const < userId:>= req; is the id regarding the current user signed in.
We simply phone the initiateChat technique from ChatRoomModel and pass it allUserIds, kind, chatInitiator . Whatever result comes we simply pass they into user.
However before we build a note we need to develop a model in regards to our chatmessages . Thus why don’t we accomplish that very first. In your systems folder make a unique file called ChatMessage.js and include this amazing contents to they:
- We a MESSAGE_TYPES item that has only 1 sort labeled as text
- Our company is determining all of our outline for chatmessage and readByRecipient
- Next we have been writing our static means for createPostInChatRoom
I understand this is exactly plenty of content material, but simply carry with me. Let’s simply write the controller when it comes to route that brings this information.
When it comes to path explained in our routes/chatRoom.js API (‘/:roomId/message’, chatRoom.postMessage) let’s visit the controller in controllers/chatRoom.js and determine it:
Read dialogue for a chat room by dating4disabled it’s id [Get request]
For the path .get(‘/:roomId’, chatRoom.getConversationByRoomId) in routes/chatRoom.js open its operator in file controllers/chatRoom.js and include the following material towards cam space:
And then at last, go to your ChatMessage model in models/ChatMessage.js and create a fixed strategy also known as getConversationByRoomId :
Mark an entire talk as read (ability similar to WhatsApp)
After the other individual are signed in and additionally they look at a conversation for a-room id, we should instead mark that discussion as review off their part.
All we have been performing here is very first examining if the area is available or otherwise not. Whether it do, we continue furthermore. We consume the req.user.id as currentLoggedUser and pass it on the preceding features:
A potential use situation is that the consumer might not have browse the latest 15 messages whenever they open up a particular space dialogue. They ought to all be marked as read. Therefore we’re making use of the this.updateMany work by mongoose.
This states i do want to find the information articles in chatmessages range in which chatRoomId suits and readByRecipients selection will not. The userId that I am driving to the features was currentUserOnlineId .
Next we should tell mongoose to not simply modify one record it locates, but additionally to revise all of the documents in which the disease fits. So doing this:
Get in touch with myself on twitter together with your feedback a€“ i might like to listen when you yourself have any recommendations for modifications: twitter/adeelibr
Any time you preferred to the article, please carry out provide the github repository a superstar and subscribe to my youtube channel.
Figure out how to code at no cost. freeCodeCamp’s available origin program possess assisted significantly more than 40,000 group bring opportunities as designers. Get going
The mission: to help individuals learn to code 100% free. We attempt by creating hundreds of clips, reports, and entertaining coding training – all free on the community. We also have tens of thousands of freeCodeCamp study organizations internationally.
Within root folder establish an innovative new folder called config . Inside that folder establish a file also known as index.js and add these information:
Here we’re telling they that firstName was of kind sequence. If the individual forgets to include this benefits while showing up in API, or if perhaps the kind is not sequence, it’ll toss an error.
We become the userId from your req.params . Should you recall through the video early in the day, req.params will be the /: identified in our paths point.
We’re going to need that user id additionally the client id (an individual’s own unique outlet id that brings about whenever they create an association with the help of our make).
We are with the make-validation library right here to confirm an individual’s request. The start API, we count on an individual to transmit several users as well as define the type of the chat-room this is certainly becoming created.