Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying WebRTC Cookbook
  • Table Of Contents Toc
  • Feedback & Rating feedback
WebRTC Cookbook

WebRTC Cookbook

By : Sergiienko
5 (1)
close
close
WebRTC Cookbook

WebRTC Cookbook

5 (1)
By: Sergiienko

Overview of this book

If you are a JavaScript developer with a basic knowledge of WebRTC and software development, but want to explore how to use it in more depth, this book is for you.
Table of Contents (10 chapters)
close
close
9
Index

Implementing a chat using a signaling server

In this recipe, we will cover the process of implementing private, peer-to-peer web chat using signaling server as the middle point. Peers will send chat messages via the signaling server. In the schema represented in the following diagram, you can see the flow:

Implementing a chat using a signaling server

How to do it…

To implement the chat feature via the signaling server, we need to add some methods to the client code with the following steps:

  1. We need to add appropriate code to the function that processes the messages from the signaling server:
    function processSignalingMessage(message) {
            var msg = JSON.parse(message);
            if (msg.type === 'CHATMSG') {
                onChatMsgReceived(msg.value);
            } else if (msg.type === 'offer') {
                pc.setRemoteDescription(new RTCSessionDescription(msg));
                doAnswer();
            } else if (msg.type === 'answer') {
                pc.setRemoteDescription(new RTCSessionDescription(msg));
            } else if (msg.type === 'candidate') {
                var candidate = new RTCIceCandidate({sdpMLineIndex:msg.label, candidate:msg.candidate});
                pc.addIceCandidate(candidate);
            } else if (msg.type === 'GETROOM') {
                room = msg.value;
                onRoomReceived(room);
            } else if (msg.type === 'WRONGROOM') {
                window.location.href = "/";
            }
    };
  2. We will check whether the received message is of the CHATMSG type and if so, we will call the onChatMsgReceived method to process it:
    function onChatMsgReceived(txt) {
        var chatArea = document.getElementById("chat_div");
        chatArea.innerHTML = chatArea.innerHTML + txt;
        chatArea.scrollTop = chatArea.scrollHeight;
    };

    Here, we will get the chat_div element by its ID and alter its content by adding the chat message received from the remote peer via the signaling server.

  3. To send a chat message, we should implement a method like the following:
    function chatSendMessage(msg) {
            if (!channelReady) return;
            sendMessage({"type" : "CHATMSG", "value" : msg});
    };

    This function checks whether the WebSocket channel is up and sends a chat message to the signaling server using the channel. To use this function, we can use the HTML input tag with the submit button and call it on the submit event.

How it works…

The basic principle of this solution is pretty simple:

  • One peer sends a text message to the signaling server, marking it as the CHATMSG type
  • The signaling server retransmits the message to another peer
  • Another peer gets the message from the signaling server, checks whether it is of the CHATMSG type and if so, shows it to the user

Tip

To distinguish chat messages from WebRTC messages, you can use any word to mark the message type. It can be CHATMSG or whatever you prefer.

There's more…

This way of implementing web chat is usually not secure because the data will go via the signaling server and not directly through the peers. Nevertheless, it is suitable for public chat rooms where there can be several people at a time. For private peer-to-peer chats, it is usually better to use WebRTC data channels, and that way it is more secure.

See also

  • To implement the chat feature using data channels, follow the Implementing a chat using data channels recipe

Create a Note

Modal Close icon
You need to login to use this feature.
notes
bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Delete Note

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Edit Note

Modal Close icon
Write a note (max 255 characters)
Cancel
Update Note

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY