I know two types of reverse AJAX:
1- Poll
2- Press
I think the survey is easier to implement, you just want your javascript to make a regular request to the server every time period, and when the server has some data for it, it will respond. It's like a ping, and some call it a heartbeat, but this is a very obvious solution to this problem. However, it can easily overload the server.
EDIT Simple Poll Example Code:
Server:
<?php
Client-Side:
function ping(){ $.ajax( { url : "pong.php", success : function (data){ data = JSON.parse(data), if (data['nothingNew']) return; for(var i in data){ var type = data[i]['type']; if (type && incomingDataHandlers[type]){ incomingDataHandlers[type](); } } }); } incomingDataHandlers = { comments: function () { $.ajax({ url: "getComments.php", method: "GET", data: getNewCommentRequsetData() // pass data to the server; success : function (data){ //do something with your new comments } }); }, message: function (){ $.ajax({ url: "getMessages.php", method: "GET", data: getNewMessageRequestData() // pass data to the server; success : function (data){ //do something with your new messages } }); } } $(docment).ready(function () { setInterval(ping, 1000); })
Amjad masad
source share