Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 36 ยท Projects and Practice

Project: A Peer-to-Peer Chat With Typing Indicators

Build a two-person chat on WebRTC data channels: a signalling stand-in, connection status, typed messages, typing indicators and delivery receipts, all running in one page.

In this project you will build a small chat that sends every message over an RTCDataChannel, with a status badge, typing indicators, and delivery receipts. Two chat windows (Ana and Ben) live in the same page, each with its own RTCPeerConnection. They connect through a tiny in-page "signalling" function that stands in for a real WebSocket server; the next projects replace it with a real one.

You will practise everything from the data channel chapter: creating channels, a message envelope, open/close handling, and a heartbeat-free protocol that stays simple. The finished chat has about 120 lines.

Design first

We use one negotiated channel (id 0) for both peers, which avoids waiting for ondatachannel and makes the code symmetric. Messages are JSON envelopes:

type Fields Meaning
chat id, text A message
ack id The receiver displayed message id
typing on The sender started or stopped typing
sequenceDiagram
  participant A as Ana
  participant B as Ben
  A->>B: typing on
  A->>B: chat id 1 "Hello"
  B-->>A: ack id 1
  Note over A: shows delivered

Every chat message is confirmed by an ack from the other side.

The code

Read the script before running it. The Peer class owns one RTCPeerConnection, the channel and its UI. The connect() function is the signalling stand-in: it forwards descriptions and candidates directly, which a real server would do over WebSocket.

Two-person chat over a data channel

When the page loads, the badge turns green, Ana sends "Hello Ben!" automatically, and "delivered" replaces "sent" once Ben's ack returns. Type in Ben's box and watch the other window show "Ben is typing...".

How it works

  • Negotiated channel. createDataChannel('chat', { negotiated: true, id: 0 }) on both sides means no datachannel event and a symmetric class.
  • Status. The channel's open and close events drive the badge and enable or disable the form.
  • Safe rendering. Received text goes through textContent, and is truncated, since a peer can send anything.
  • Acks. Each outgoing message id is stored in a Map; the ack resolves it. This is the same idea as the request-response pattern in Protocols and Patterns.

Ideas to extend it

  1. Reliable and unreliable together: send typing indicators on a second channel with ordered: false, maxRetransmits: 0, since a missed typing update is harmless (Reliability and Ordering).
  2. Message history on reconnect: keep messages in memory and resend unacked ones after an ICE restart.
  3. Rate limit incoming messages so a hostile peer cannot flood your page.
  4. Sizes: reject messages over, say, 2,000 characters before sending.
  5. Real signalling: replace connect() with a WebSocket exchange; that is the next-but-one project.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. Why does the chat use a negotiated channel with id 0?

    With negotiated: true and the same id on both sides, the channel simply opens when the transport is ready.
  2. 2. Why is received text added with textContent rather than innerHTML?

    Peer data is untrusted input. Inserting it as HTML would allow cross-site scripting.
  3. 3. What does the ack message do?

    The ack carries the id of the message it confirms; the sender looks it up in its pending map.

Summary

  • A chat needs only a data channel, a small message envelope and a status model.
  • Negotiated channels keep two symmetric peers simple.
  • Use acks for delivery, typing for presence hints, and textContent for safety.
  • The signalling stand-in is the only part that must change for real networks.

Next: Project: File Transfer With Progress and Checksum