Typing Indicators

In the previous chapter, we used long polling to get a new message onto the screens of the room’s other members. Now students ask for one more feature:

  • When a member types in a room, Courselore must show the room’s other members who have it open that the member is typing.

For example, while Priya types in Team 4’s room, Marcus sees “Priya is typing”.

Typing indicators with long polling

To build this, Priya’s client has to tell the server that Priya is typing, and the server has to pass that on to Marcus’s client. Let’s try it with what we already have: HTTP requests and long polling.

While Priya types, Priya’s client sends the server a short note that says Priya is typing in Team 4’s room. We call it a typing event. The client sends one at most every 3 seconds, not on every key press. With HTTP, a client can send something to the server only in a request, so each typing event is one request:

POST /rooms/12/typing

The server passes each typing event on the same way it passes on a new message. Marcus’s client keeps one long poll going for course 7, GET /courses/7/updates?after=.... When Priya’s typing event arrives, the server answers that long poll. The response now has a second list, typing, next to messages:

{
  "messages": [],
  "typing": [
    {
      "room_id": 12,
      "user": {
        "id": 7,
        "name": "Priya Shah"
      }
    }
  ]
}

Marcus’s client shows “Priya is typing” in Team 4’s room and sends a new long poll at once. A typing event has no message identifier, so the new long poll carries the same after. The server does the same for every other client that has the room open.

How many requests

How much work does this add at the server? At the busiest hour, about one message is posted a second. Writing a message can take anywhere from a few seconds to a few minutes: you revise it, look something up, paste a link, or fix the formatting. Assume it takes about 50 seconds on average. Then about 50 people are typing at any moment. Each of them sends a typing event every 3 seconds, so the server receives about typing events a second. That is 17 requests a second.

In the previous chapter, we worked out that at most 10 clients have a room open. So each typing event ends about 10 long polls, and each of those clients sends a new long poll. That is requests a second.

How many requests did the server receive before typing indicators? When nothing is posted, each of the 600 open clients sends a new long poll every 30 seconds. That is requests a second. About one message is posted a second, and it ends the long polls of the at most 10 clients that have its room open. That is about 10 more. So the server received about 30 requests a second. Add this to the 17 typing events plus the 170 more requests we worked out earlier, and you get about 220 requests a second.

Each of these is a full HTTP request. It arrives with its headers and credentials, and the server authenticates it. The headers of a request are commonly 700 to 800 bytes. The typing event itself, which room and who is typing, is a few bytes. So almost everything the client sends for a typing event is headers.