Sending Messages in the Chatroom

Let's try sending messages in the chatroom using RxJS operators like "withLatest" and "combineLatest"

Sending messages

Now that the user can see the current rooms and the messages sent to them, it’s time to let them send messages of their own.

Compared to the two sections in the chat room so far, sending messages is fairly simple.

Sending chat room messages using merge

It starts with the same technique as the login modal, using merge to listen for either a selection of the “Send button” or a press of the “Enter key”.

Next, the stream plucks out the value of the message box, ensures the value is not an empty string, and resets the message box.

withLatestFrom operator

The following snippet introduces a new operator you haven’t seen before: withLatestFrom.

The stream in this snippet needs to send a new chat message (entered by the user) to the server and needs to annotate it with the user’s name and current room so the server knows who sent the message and where it was sent.

Get hands-on with 1200+ tech skills courses.