General Question

Ranimi23's avatar

How is “Someone is typing a comment...” implemented in Facebook?

Asked by Ranimi23 (1917points) October 11th, 2016

This is a new feature Facebook added, like each post now is a chat room and you can see in real time when someone is writing comment. How? Is there a communication channel open that checks every X seconds if there are comments being written and raise a flag that displays it to me? Interesting to know how it can be done in web programming. Do you think is it the same way fluther tells us when someone write answers?

Observing members: 0 Composing members: 0

2 Answers

Mariah's avatar

I am a software engineer; I do not know how Facebook actually implements this but here’s how it could be done:

Say you’re at your computer looking at a Facebook post. When you start to type in the comment box, your browser knows that the contents of the box have changed. Usually, this information does not need to be communicated to Facebook’s servers in order to “publish” this comment until you hit the “post” button. However, to implement the feature you described, you could bind an “action listener” to the text box. This is basically a function that gets triggered any time the contents of the box change. The action listener could then send an ajax call up to the server. Ajax calls are asynchronous (that’s what the first A stands for), meaning that the call can happen “in the background” while you’re still doing whatever you’re doing, so it doesn’t interrupt your typing. The ajax request can then go talk to the server and give an indication that the contents of the text box are changing. So, we know this probably means someone is typing.

In order to optimize this so that the server doesn’t get bogged down with billions of requests every second, I would guess that the action listener filters out most changes before talking to the server. That is, we don’t care if the contents of the text box just changed from “Hi” into “Hi Judy,” but we do care if the text box just went from empty to non-empty, or vice versa, as these are the indicators that someone has either just started or just stopped typing.

Zaku's avatar

In addition to what Mariah said, Facebook has several other things it does which already send updates out to clients after a page is loaded. Such as the “lose your place because a new post appeared” feature. So info about others writing comments could be attached to these updates without having to add any new events than they already do.

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther