Motyar

Simple Chat Application with HTML5 Server-Sent Events

Lets talk about HTML5's one of fancy feature Server-Sent Events that can be defined as

"API for opening an HTTP connection for receiving push notifications from a server".

Why Server-Sent Events when there is WebSockets?

WebSockets is richer protocol to perform bi-directional, full-duplex communication. WebSockets are useful where we need two-way, real-time updates in both directions; and can be used in Games, chat app.

When we simply need updates from some server action, like updating news, stocks or feeds. Server-Sent Events are your friend. SSEs have special features like automatic reconnection, event IDs, and the ability to send events. And doesn't require full-duplex connections and new Web Socket servers to handle the protocol.

Idea

In this post we will talk about the basic parts, implementation and using it to write a simple chat application. We ll be using PHP on server side.

JavaScript API


var source = new EventSource('/news/');
    source.onmessage = function(e) {
        alert('We got new news:'+e.data);
    }

Server

Server must send plaintext response, served with a 'Content-Type: text/event-stream'

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
function sendMsg($id, $msg) {
  echo "id: $id" . PHP_EOL;
  echo "data: $msg" . PHP_EOL;
  echo PHP_EOL;
  ob_flush();
  flush();
}

$serverTime = time();

sendMsg($serverTime, 'server time: ' . date("h:i:s", time()));

Simple chat application

I wrote a simple chat application using this technique. You can see a demo here.


Share the post if you enjoyed reading and think its useful.

Labels: , , , , , ,

By :

14 comments

#

Anonymous John says…

awesome post and awesome blog sir!
 
#

Anonymous Avatar Html5 Video Player says…

Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
 
#

Blogger Obscuro Luar says…

Hi, I've developped a chat but did not work well with firefox timeout, so I took the "sendMsg" function that makes firefox work with each click, also works fine. I made some adaptations to insert the time, for example (1 hour ago ...) but I also had to use mysql. Thanks from one brazilian life in Italy.
 
#

Blogger Manoj K says…

Nice work...., Motyar :)
 
#

Anonymous hitesh says…

Nice work.. keep it up ..
 
#

Blogger Paul Jay says…

Nice work. Safari in Windows doesn't work well. It crashed window but works find in Chrome browser.
 
#

Anonymous Anonymous says…

nice
 
#

Anonymous Anonymous says…

where i found the code?

 
#

Anonymous Anonymous says…

Great post and nice source. Just wanted to point out that after some playing with this I found out what I usually end up with... Server Sent Events are not yet supported in Internet Explorer (not even IE9). *Facepalm*

Otherwise, works great in Chrome and Firefox. I've also been reading up and playing with your source and see that if you modify the server and add: echo "retry: 1000" . PHP_EOL; -- Then you will get responses much quicker than the default of "about 3 seconds".
 
#

Blogger Motyar D says…

Thank you everyone.
@Anonymous its retry: 100 in demo. thanks for tip.

@ ಶ್ರೀಪತಿ ಗೋಗಡಿಗೆ with which browser you are trying?

@Paul Jay added fallback, it should work with all browsers now.

@hitesh @manoj @john thanks guys.


 
#

Anonymous Nico says…

Great stuff, but it does not seem to work from an android opera browser: impossible to click on 'enter' to enter the text...
 
#

Blogger SA says…

nice work,but did not work on ie6.
 
#

Blogger Deepak Sharma says…

Hi,
can you please assist me?
I have a problem as we know in server sent event it automatically sent the push msg to client after every 3 sec, and we can change this time.
But my problem is I want to trigger this sending msg to client on database update. Please help me out.
consider
File1 - which subscribed the event source to the file2
file2 - send the live stream to client a php file.
file3 - has a function, used to insert the value in DB.

now I want if I call function in file3 to insert the value only then file2 send the msg to clients..

Please help me out..
 

Post a Comment