[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 775
  • Last Modified:

Real time chat and notifications

Hi,

I want to create real time chat and notifications exactly as facebook.

What way facebook use and how can I do the same?
0
Mohamed Abowarda
Asked:
Mohamed Abowarda
  • 4
  • 3
  • 2
  • +2
4 Solutions
 
iGottZFrontend EngineerCommented:
0
 
iGottZFrontend EngineerCommented:
well if this all is just too much for you you should may try JQuery wich comes directly with json / ajax support for all browsers
0
 
Mohamed AbowardaSoftware EngineerAuthor Commented:
I have created PHP backend script and I am using infinite loop, once the server find available data for pushing to the client, my code is as the following:

<?php
while(true)
{
// Check if any data is available to push to the client
sleep(1);
{
?>

Open in new window


On page load, the client make HTTP request (AJAX) and the server delay the response till there is available data.

Is that how Comet works?
Can this affect on the server processor?
What's your recommendation about what I am doing above?

Thanks,
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Ray PaseurCommented:
Why not just use Google Chat?  You don't need to reinvent this wheel - there are free solutions already released into the wild.

Regarding this, "Can this affect on the server processor?" the answer is "Yes, every time a client makes a request to the server it has an affect."
0
 
Mohamed AbowardaSoftware EngineerAuthor Commented:
> Can this affect on the server processor?
I mean can this affect on the server processor far over average or not.

> Why not just use Google Chat?
Google Chat is instant messenger, what are you referring to?

I want to know if the way I posted above is how Comet works or not.

Thanks,
0
 
Slick812Commented:
greetings Medo3337, , , You do not seem to know how a browser using the javascript XMLHttpRequest( ) , sometimes called AJAX, and your server XMLHttpRequest page, as a PHP page, are set up to function and exchange information.
In your code -

<?php
while(true)
{
// Check if any data is available to push to the client
sleep(1);
{
?>

you do NOT understand that server side scripts in PHP can NOT push data to any browser, the web connection methods for web servers are for processing "Requests" for pages, images, xml, ,  and then from the page-image request from a browser (or other request source) it sends the information (page, image, etc.) to the browser.

AJAX is used in very many of the "Real Time Chat Packages" for many of the server scripting languages (PHP, ASP, JSP), you can find some ajax chat for PHP in many places, if you want to use code someone has already produced and tested.

as to your question, you can set the wait time for a javascript XMLHttpRequest( ) to be relatively long period of time (in relation to computer time), and this will sort of simulate the server side code "pushing" data to the browser.
You may want to read this web page to find out some more about it -


http://stackoverflow.com/questions/1086380/how-does-facebook-gmail-send-the-real-time-notification

but that kind of "simulate the server side code pushing data" may not be something you would need to do, unless you had the time and knowledge to implement a custom chat and notifications package. you might save yourself time by using a php chat package, that is already available to download and use.
0
 
SamMacPCCommented:
Specifically, Facebook now uses Jabber.

Jabber is open source and there is plenty of example code to look at at jabber.org, and there are web-bad Jabber clients.

Google Talk also uses Jabber.

You may have noticed in the early days Facebook chat was kind of unreliable. Their switch to open source, standards based Jabber technology allowed them reliability.

Meebo.com is free as in beer and you can have a chat module that supports all of the major chat networks, including Facebook on your site.

http://www.meebo.com/websites/

http://business.meebo.com (for a custom one that costs money.)

Å very high profile site that uses the "Meebo bar" which looks very similar to Facebook is AllThingsD.com, run by Kara Swisher and Walt Mossberg, technology columnists for the Wall Street Journal.

0
 
Mohamed AbowardaSoftware EngineerAuthor Commented:
@Slick812: I know how the browser use javascript XMLHttpRequest (AJAX).
I am not sure if understood my what I am doing, basically, on page load (client) the javascript make AJAX request for updates, the server delay the request and keep the connection and the process running till there is data pending in the database, if there is I use echo() to response to the client and terminate the connection, once the client receive the response it process the data and make the same AJAX request again, and so on...

My question now, Does this way will work perfectly on average webservers?
Since sometimes 50,000 PHP processes will be running at the same time and executing database query every second (if there is 50,000 member online), can this way use huge amount of CPU or slowdown the server?

@SamMacPC: I am still not sure, do you mean that facebook was using Comet and now switching to Jabber for better performance?
0
 
Slick812Commented:
I was somewhat interested in the idea of setting a long wait for a XMLHttpRequest and then doing some sort of new data available testing loop in the PHP response page, maybe with a sleep(1), for a PHP chat set up, as you have said, this should reduce the traffic and bandwidth, and maybe the PHP processor usage, I do not have the time now to build a test chat site to see if that is true or not. But like all ideas and concepts, the performance increase or decrease is in the code details, as how you set up the XMLHttpRequest access, and the javascript timing, and whether you use only one PHP page for all chat XMLHttpRequests, or have two or more PHP pages to handle the send new chat entry or post, and have php pages to do the updates for others new posts, or new user join chat.
I do not know anything directly about the facebook real time chat and notifications, but I did do some web searches, about those subjects, and there were several many pages that talked about facebook switch to Jabber for their chat, and the Jabber also seems to have alot of usefull options, and the real time notifications, are listed as being handled by the Jabber IM.

I do not know if you are aware of it, but facebook has a developers forum, blog and documentation site, here is a link you can look at about the Jabber chat thing-
http://developers.facebook.com/blog/post/105

they also have a search for the facebook  developers site, and there may be some specific information about the way you want to do your chat. .

Jabber seems to be a complex and many optioned software package, maybe it can work for you, although it is made to be an instant messenger extention, but it does other stuff as well.

Just my thoughts on performance in chat. I was asked to change a php chat setup because it was slowing down their sites MySQL database.
In their javascript they requested a XMLHttpRequest update 10 times a second. I changed it to update every 2,2 seconds, and this made a difference in the database performance, I looked at the php database code for SELECT and UPDATE, and they did not use any SQL limits or other sql synax to alter the SQL calls, . so I changed the code to not use a database to store the BLOB fields they had for the chat posts. I used a php array to organize the chat data, like last chat update time, last user update time, and the chat and user <div> blocks of HTML text. I saved the array as a file with serialize, and had a separate file for each chat room or chat page, that usually had 20 or less chat clients on each one. but this eliminated the MySQL database slowdown, and worked well for the amount of users (traffic) they had for that site. You may not be able to not use a database, but this made A big difference for them.
0
 
Mohamed AbowardaSoftware EngineerAuthor Commented:
Does using XMLHttpRequest is better and/or faster than comet iframes?
0
 
iGottZFrontend EngineerCommented:
Does using XMLHttpRequest is better and/or faster than comet iframes?
let me tell you the differences.

i will call XMLHttpRequests in this explaination simply: Ajax.

what Ajax does is simply explained.
you create a http header wich includes stuff like the url you want to access, the variables you want to send, current browser cookies, referral and way more things.
this header will then be send.

when you as example create a html <form> and post something through that to a php file you kind of do the same ajax can do in the first place.

you send data to the server wich the server then can work with.

the difference betwean a <form> and ajax is, that ajax allows you to fetch the data that is behind the url you are requesting and returns it to its onreadystatechange property instead of showing the content on a new page or within an iframe.

through that you can call a function that then as example prints a message into a message window.


in your case i would use ajax with jquery since this would allow cross browser compatiblity from IE 6 mobile for windows mobile to Chrome 11 and even opera mobile


comet
does nothing else then using an serversided infinite loop to print updates directly into an iframe on your site

the advantages of this are huge.

when the iframe gets created, your browser will send a HTTP Header ONCE to the server to request its content.

then the server can write into this iframe whenever he wants since the iframe will not timeout (though you could create checking functions for ip refresh's etc.)

the best part of this is, that the server can then send data with time offsets without the need to get extra http queries.

also comet is more efficient since the server runtime then only need to execute once and then can run till the session breaks.
(initializing server runtimes like a machine gun also requires computing)



so.. comet = less traffic / higher efficiency
ajax = generaly required in order to send data to the server / less efficient in reseiving data then comet but can catch errors nearly instantly



the best you could use is websockets but thats not implemented yet by most browsers. also it lacks of security why some browsers have it but havnt enabled it by default.

websockets would allow you to create a connection betwean you and the server once and then query data to it and reseive data from it without loosing connection.



you need ajax anyways for sending data to the server.
if you use ajax or comet to reseive data will be your choise


since some days i'm writing on a chat completely implemented in javascript and php using ajax.
right now it looks like this:
http://gyazo.com/c4b9e9ef4531cb6237019704e9d371f7.png
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now