ajax push server - ajax

Alright, I recently wrote a ajax push script which had php on the backend sleeping while waiting for someone to make an update. However, the sleeping processes took up a lot of cpu. Any ideas on how I can prevent this? I am guessing I will have to either find a program which can do the sleeping with threads or write my own in python or c++ which I am not very familiar with. I am using ajax push for a browser based game so people can play it in real time but I think if I get too many sleeping processes on the backend it would overload the server.

If I understand correctly, you want some kind of long-polling stuff -- you should search for the term "comet".
For instance, here are a couple of posts that might interest you :
Is there some way to PUSH data from web server to browser?
Online tutorials for implementing comets (server push)
Using comet with PHP?
Problem of choosing comet server
Comet & PHP: How to use Comet with a PHP Chat System ?
Still, one might say that PHP, because of its non-threaded model, I suppose, is not the best language/technology for that kind of stuff... And maybe you'll end up looking to another technology for that part of your site...
For instance, you can take a look at :
Choosing and deploying a comet server.

some links about reverse http:
reverse http 1
making-real-time-web-real-time
reverse http 2
rest-requires-asynchronous-notification

After much research last night, I found this as the answer for the server side.
Then write a php script for the client side.
Then have ajax call the client which calls the server.
Only problem is some of the extensions have to be manually installed.
http://php-mag.net/itr/online_artikel/psecom,id,484,nodeid,114.html

Related

What technologies are used in a seemingly 'live' data driven website?

Maybe the title could be rephrased slightly better but basically I'm wondering how the likes of facebook have implemented a 'live' interface with regards to new notifications/messages etc. I know that the complexity behind such a social network is too much to discuss in this one small SO thread but if anyone has any idea as to the technologies used in order to notify it's users almost immediately of new records (I'm assuming DB records) then I'd be curious to hear it.
If I were to guess, I'd say that there were timers on client-side code that would periodically check the database via AJAX and then react accordingly. Is this right?
It is via Comet and long polling via node.js or similar non-thread based web servers.
If I were to guess, I'd say that there were timers on client-side code that would periodically check the database via AJAX and then react accordingly. Is this right?
You are partially right. The client opens a connection and the server only responds once it has something to return to the client.
I think they are trying to use HTML5 WebSockets but as a fallback they using Comet, AJAX, Long pooling with a good backend.

How do comments appear instantly on Facebook?

I was just wondering, How do comments appear instantly on Facebook? For example, when I'm on my profile and my friend comments something on my post, I can instantly see it. Is it AJAX? Or Queuing system? If I want to do the same thing, what do I do?
Thanks
I'm not exactly sure how facebook has implemented their system.
but it will either work with websockets, AJAX or a comet server.
If you want to have the same effect there are a lot of different techniques you could use,
but I would recommend looking into node.js and maybe even the now.js plugging, which allows for realtime updates via websockets. It even has support for older browsers, so if the browser does not support websockets, it will do a fall over to either a comet server implementation, AJAX or an iframe.
Basically websockets allow for better control over when data should be sent or received from and to the server since it constantly listening to the socket, so you only send data when required and same for receiving data as well, where with an AJAX approach you had to make a call every X seconds.
It's extremely easy to setup on a linux environment, and there's ample documentation to get you started.
It works with javascript and is build on the Google V8 engine, so if you've ever worked with OOP Javascript, you should be able to pick it up relatively easy.
LINKS:
http://nodejs.org/
http://nowjs.com/
You'll want to look into PHP sockets
Actually, its long polling according to this answer (which also explains how to verify or see if its changed since the answer):
How does Facebook fetch live updates

AJAX optimization - instead of checking every 2 ms?

Heylo guys
i am writing an experimental AJAX page that checks the data of a txt file every 2 ms and if there is a change, post the change to the page
problem is that this is very inefficient bandwidth wise and computation-wise. Is there another way to do this? I have heard of websockets (not familiar though)
thx!
You can use COMET
http://en.wikipedia.org/wiki/Comet_%28programming%29
AJAX Push (Comet server) is exactly what you need. This is a very powerful tookit/engine: http://www.ape-project.org/
Instead of comparing the data, you could only compare the file size/last modified date on your server side script and return a 0/1 depending on if it didn't change or did.
Apart from that you'll have to tell us more about which language you're using on the server for us to be able to tell you what to do. You can use Long Polling or Comet, but it really depends on your server side language how good those would be. With PHP for example, I've found these to not be possible unless I installed stuff on the server, which in shared hosting for example is not possible.
Checking a text file 500 times per second is a bad idea, IMHO.
My suggestion is to have your server-side script check the modified date and "push" a notification to your AJAX listeners. Node.js and Faye would be good for this.
A good screencast on Node can be found here ($12):
http://peepcode.com/products/nodejs-i
That screencast also covers Faye (http://faye.jcoglan.com/node.html). Faye simplifies the whole Comet/polling process.

WebSocket cross-connection communication (Tornado?)

I'm fumbling around a bit with WebSockets, and was pretty pleased with how easy it was to get a Tornado server running that does basic websocket connections. I've never used Tornado before today, and while I like what I've seen there's a few questions that I have regarding it's use.
Primarily, I'm using WebSockets so that I can have low-overhead communications between two or more client machines. (For the purposes of conversation let's just say it's a chat client) Obviously I can connect into the server from multiple machines, and they can all push messages to the server and the server can respond, which is great! But that's not too much better than your standard AJAX requests. If I have a persistent connection I want to be able to push data to the clients as well. The simplest possible scenario is user 1 posts a message to the server and upon receiving it the server immediately pushes it to user 2.
So what would be a good way to accomplish that? As far as I can see in Tornado there's no way to communicate between connections other than placing the message in a datastore somewhere and having all the other connections poll for new info. That strikes me as terribly clunky though, because all you're really doing at that point is moving the polling process from the client to the server.
Of course, I may be barking up the wrong tree entirely here. It's certainly plausible that Tornado simply isn't the right tool for this job, and if that's the case I'd be happy to hear suggestions for alternatives!
Here is a chat server using tornado, WebSockets and redis: https://gist.github.com/pelletier/532067 (Updated: link fixed, thanks #SamidhT)
Though the answer has already been accepted: Using a different service still seems very inefficient to me. Why don't you just go with shared memory + conditional variables / semaphores? You sound like you got a standard Consumer-Producer problem

Is there some way to PUSH data from web server to browser?

Of course I am aware of Ajax, but the problem with Ajax is that the browser should poll the server frequently to find whether there is new data. This increases server load.
Is there any better method (even using Ajax) other than polling the server frequently?
Yes, what you're looking for is COMET http://en.wikipedia.org/wiki/Comet_(programming). Other good Google terms to search for are AJAX-push and reverse-ajax.
Yes, it's called Reverse Ajax or Comet. Comet is basically an umbrella term for different ways of opening long-lived HTTP requests in order to push data in real-time to a web browser. I'd recommend StreamHub Push Server, they have some cool demos and it's much easier to get started with than any of the other servers. Check out the Getting Started with Comet and StreamHub Tutorial for a quick intro. You can use the Community Edition which is available to download for free but is limited to 20 concurrent users. The commercial version is well worth it for the support alone plus you get SSL and Desktop .NET & Java client adapters. Help is available via the Google Group, there's a good bunch of tutorials on the net and there's a GWT Comet adapter too.
Nowadays you should use WebSockets.
This is 2011 standard that allows to initiate connections with HTTP and then upgrade them to two-directional client-server message-based communication.
You can easily initiate the connection from javascript:
var ws = new WebSocket("ws://your.domain.com/somePathIfYouNeed?args=any");
ws.onmessage = function (evt)
{
var message = evt.data;
//decode message (with JSON or something) and do the needed
};
The sever-side handling depend on your tenchnology stack.
Look into Comet (a spoof on the fact that Ajax is a cleaning agent and so is Comet) which is basically "reverse Ajax." Be aware that this requires a long-lived server connection for each user to receive notifications so be aware of the performance implications when writing your app.
http://en.wikipedia.org/wiki/Comet_(programming)
Comet is definitely what you want. Depending on your language/framework requirements, there are different server libraries available. For example, WebSync is an IIS-integrated comet server for ASP.NET/C#/IIS developers, and there are a bunch of other standalone servers as well if you need tighter integration with other languages.
I would strongly suggest to invest some time on Comet, but I dont know an actual implementation or library you could use.
For an sort of "callcenter control panel" of a web app that involved updating agent and call-queue status for a live Callcenter we developed an in-house solution that works, but is far away from a library you could use.
What we did was to implement a small service on the server that talks to the phone-system, waits for new events and maintains a photograph of the situation. This service provides a small webserver.
Our web-clients connects over HTTP to this webserver and ask for the last photo (coded in XML), displays it and then goes again, asking for the new photo. The webserver at this point can:
Return the new photo, if there is one
Block the client for some seconds (30 in our setup) waiting for some event to ocurr and change the photograph. If no event was generated at that point, it returns the same photo, only to allow the connection to stay alive and not timeout the client.
This way, when clients polls, it get a response in 0 to 30 seconds max. If a new event was already generated it gets it immediately), otherwise it blocks until new event is generated.
It's basically polling, but it somewhat smart polling to not overheat the webserver. If Comet is not your answer, I'm sure this could be implemented using the same idea but using more extensively AJAX or coding in JSON for better results. This was designed pre-AJAX era, so there are lots of room for improvement.
If someone can provide a actual lightweight implementation of this, great!
An interesting alternative to Comet is to use sockets in Flash.
Yet another, standard, way is SSE (Server-Sent Events, also known as EventSource, after the JavaScript object).
Comet was actually coined by Alex Russell from Dojo Toolkit ( http://www.dojotoolkit.org ). Here is a link to more infomration http://cometdproject.dojotoolkit.org/
There are other methods. Not sure if they are "better" in your situation. You could have a Java applet that connects to the server on page load and waits for stuff to be sent by the server. It would be a quite a bit slower on start-up, but would allow the browser to receive data from the server on an infrequent basis, without polling.
You can use a Flash/Flex application on the client with BlazeDS or LiveCycle on the server side. Data can be pushed to the client using an RTMP connection. Be aware that RTMP uses a non standard port. But you can easily fall back to polling if the port is blocked.
It's possible to achive what you're aiming at through the use of persistent http connections.
Check out the Comet article over at wikipedia, that's a good place to start.
You're not providing much info but if you're looking at building some kind of event-driven site (a'la digg spy) or something along the lines of that you'll probably be looking at implementing a hidden IFRAME that connects to a url where the connection never closes and then you'll push script-tags from the server to the client in order to perform the updates.
Might be worth checking out Meteor Server which is a web server designed for COMET. Nice demo and it also is used by twitterfall.
Once a connection is opened to the server it can be kept open and the server can Push content a long while ago I did with using multipart/x-mixed-replace but this didn't work in IE.
I think you can do clever stuff with polling that makes it work more like push by not sending content unchanged headers but leaving the connection open but I've never done this.
You could try out our Comet Component - though it's extremely experimental...!
please check this library https://github.com/SignalR/SignalR to know how to push data to clients dynamically as it becomes available
You can also look into Java Pushlets if you are using jsp pages.
Might want to look at ReverseHTTP also.

Resources