DEV Community


Posted on

Asynchronous, Bidirectional Connected Chat App with SSE in PHP

Yesterday, I created a simple, useless chat app to study the asynchronous bidirectional connection with PHP.

You can use it here, and get the source code here (I don't understand enough what it means to disclose the server-side script. I wanna show the beginners an example with SSE and believe there's no vuln and no one wouldn't do something bad :))

At first, I wanted to learn WebRTC and coded, but the code didn't work because of the hard restriction of my shared server. So, I tried some techs, and I found the SSE works properly.

What is the asynchronous / bi-directional connection ๐Ÿ™„ ?

Usually, you use the synchronous, server->client connection while surfing the Internet. This post is the well-explained this type of connections, this is called HTTP.

In contrast to that, the asynchronous connection allows you to talk to the server without your any actions.

And the bidirectional connection allows the server to talk to you without your any actions.

These special connections are required when you want to create a real-time web application with lesser amount of the transferred data.

Technologies ๐Ÿ˜Žย 

There are several techs to support asynchronous/bidirectional connections.

What technology will you use when you want to create a chat app? The Websocket Protocol (bidirectional, asynchronous connection between the server and the client) or WebRTC or the basic client-server-database architecture is the common way to do that. I've tested WebRTC and The WebSocket Protocol and these ways didn't work properly in this server due to the hard restriction.

Let's check them a bit.

Ajax ๐Ÿ˜‹

Asynchronous connection with JavaScript.

WebRTC ๐Ÿคฉ

This is a group of specifications to do P2P connection (client<->client, async). In PHP, there's some great libraries to do it easily:


WebSocket ๐Ÿ“ฐ

The WebSocket Protocol is a protocol to do async, bi-directional connection.

Comet ๐Ÿ’ซย 

This is a hack to manage to allow the server to talk to the client asynchronously, works over HTTP. The point is keeping from responding for a long time.

First, the client send the request to the server. Although usually the server respond to it immediately, in this time, the server keeps from responding. Then the server uses the kept response when it has something has to tell the client (event). The client gets the response and send the request again for the next event. Very nice idea!

However, there even are some problems in Comet. For example, what if the event happens while the previous event is transferred ? It would cause a lag.

SSE ๐Ÿ˜ฒย 

SSE (Server-Sent Events) is an another tech to do async, server->client connection over HTTP. It mightn't be well-known, but the specification is very simple and it's very easy to implement.

This is an evolved Comet. First, the client sends the request to the server, and the server keeps from responding. When an event happens, the sever send the response but as a chunk data. So the connection won't terminate.

Summary ๐Ÿ‘

I used SSE and it works properly. Now, I can use asynchronous, bidirectional connection even in shared servers!

Although it's not complete, I think the SSE is a good alternative to the WebSocket. And it should be used more.

To implement, I recommend you to read the very simple specification and some great blogs. And you may check my code for your reference.

Thank you.

Top comments (6)

nielsbaloe profile image

I used it yesterday to make webRtc working on a shared PHP server. It works perfectly, although yes indeed I could not find this anywhere else on the web, nobody has done this before appearently. I'll even dare to use it in production when it is for short term or non-payed projects. See . Enjoy!

volomike profile image
Mike Ross ๐Ÿ‡บ๐Ÿ‡ธ

Three questions I have with this:

  1. Can this be forked and changed to be a simple text chat app? So, instead of video/audio sharing, one does real-time sharing of text chat (think IRC chat)?

  2. Your example I assume is peer to peer. (It's late tonight and I don't have a buddy to test with until they come online tomorrow.) Can this be multi peer? So, for instance, 5 guys load this same web page and can chat with each other and see what everyone else has typed, not just a 2 person thing.

  3. What are the functions of the server besides handshake and downloading the source code? Is the rest done peer-to-peer? If so, then that would be ideal because that means that thousands can chat in realtime without loading down server resources. So, that's an interest of mine.

volomike profile image
Mike Ross ๐Ÿ‡บ๐Ÿ‡ธ

And, not related to this, I wanted to suggest to you another PHP-based minimalist framework:

Painless -

...for your Github list you have on minimalist frameworks.

sergeytelpuk profile image

Hi, how about Browser Support?

creativegp1 profile image


Admittedly, Internet Explorer and Edge don't support EventSource though they support WebSocket. However, Microsoft insists on using Edge instead of IE, and Edge is in heavy development now. So I think that Edge will support EventSource someday.

Having said that, there might be no person to dare to use EventSource if there were no server restriction for now.

sergey_telpuk profile image
Sergey Telpuk

I agree, event source is a feature wich will never be used in production.(from my side exactly)