Structuring Chat Messages in JSON

Instead of sending messages as unadorned strings, as we did in the last chapter, let’s use JSON to structure some metadata with each message sent over the chat. To start with, the remote peer will use the metadata’s content to acknowledge each message received. We can even add a little CSS so that, on the sender’s side, there is a visual difference between messages that have been received by the remote peer and those that have not.

Preparing and Sending JSON

Sending JSON isn’t much different from sending strings: JSON, or JavaScript Object Notation, is a fancy kind of string. Let’s rework handleMessageForm() so that it builds a small object literal on message in place of the message strings we relied on in the ...

Get Programming WebRTC now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.