Creating Real Time Web Updates With AJAX PHP SQL Etc. ?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 5964 Views - Last Post: 23 April 2013 - 08:57 PM

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

Creating Real Time Web Updates With AJAX PHP SQL Etc. ?

Posted 20 April 2013 - 03:20 AM

This is a pretend example but this will help answer a lot of question I have.

So lets say you have news stores in an SQL database and you're back end is PHP. When someone is at your homepage WITHOUT refreshing the page you want news updated to appear automatically while they are idling on the page.

One thing you could do is use Javascript and set a time interval to check for updates every say 30 seconds and if they are idling and a new page is posted BAM JQUERY appends another page with some slick animation just for fun too...ok that all makes sense for development...now

someone told me though that if your sites gets big enough you'll want to use a comet server which is basically using AJAX PUSH.

I have no idea or semblance or understanding of this whole AJAX PUSH thing and why that would be faster than just using periodic setinterval timing checks to check for new content?

I thinks this comes from a lack of not understanding the technology of ajax push I'm not envisioning it. The standard jquery post makes perfectly logical sense to me but not this whole ideas of AJAX push. What is actually happening and how in the name of shiva could that possible end up appending updated content to the page.

When someone posts an new news article using my example it goes into the SQL database lets say; so it's stored there. Now how technologically or can someone just explain how AJAX push is going to get that out of the database? Someone told me though it wouldn't have to make an SQL database query but rather it would be pushed automatically? This makes no sense to me. Thanks in advance guys!!

Is This A Good Question/Topic? 0
  • +

Replies To: Creating Real Time Web Updates With AJAX PHP SQL Etc. ?

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,249
  • Joined: 08-June 10

Re: Creating Real Time Web Updates With AJAX PHP SQL Etc. ?

Posted 20 April 2013 - 03:33 AM

View Postadn258, on 20 April 2013 - 12:20 PM, said:

I have no idea or semblance or understanding of this whole AJAX PUSH thing and why that would be faster than just using periodic setinterval timing checks to check for new content?

AJAX PUSH means that the server is sending data to the client, without a prior client Request.

but if you’re that far, you could also explore Socket connections (e.g. WebSockets, more general information via google/wikipedia …) like those used by socket.io.
Was This Post Helpful? 0
  • +
  • -

#3 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

Re: Creating Real Time Web Updates With AJAX PHP SQL Etc. ?

Posted 20 April 2013 - 12:59 PM

So What you're saying about these socket API's like the one you showed me i.e. how you could use it is when someone posts a news article (using my example again) apparently you can have the server on a totally different page correct? So you could just push it to the news headlines on the home page for example without the need for pulling anything out of the Database. Am I Correct?

This post has been edited by Dormilich: 20 April 2013 - 02:04 PM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,249
  • Joined: 08-June 10

Re: Creating Real Time Web Updates With AJAX PHP SQL Etc. ?

Posted 20 April 2013 - 02:07 PM

you don’t have a server on another page. if you post a message to the server, then the server can distribute that message to all (other) connected clients. and since it does not use HTTP, you don’t have to wait for a client request to send data to a client.
Was This Post Helpful? 0
  • +
  • -

#5 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

Re: Creating Real Time Web Updates With AJAX PHP SQL Etc. ?

Posted 21 April 2013 - 01:56 AM

So if you have a client on your home page receiving newly posted news articles this would work then right? This would also likely be faster than checking your SQL database all of the time for new posts again updating idle people on your home page providing them real time content.

This post has been edited by Dormilich: 21 April 2013 - 01:27 PM

Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,249
  • Joined: 08-June 10

Re: Creating Real Time Web Updates With AJAX PHP SQL Etc. ?

Posted 21 April 2013 - 01:30 PM

View Postadn258, on 21 April 2013 - 10:56 AM, said:

This would also likely be faster than checking your SQL database all of the time for new posts again updating idle people on your home page providing them real time content.

that mainly depends on the backend (language) you use. but generally you wouldn’t wait for a DB update, you would wait for an incoming message.
Was This Post Helpful? 0
  • +
  • -

#7 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

Re: Creating Real Time Web Updates With AJAX PHP SQL Etc. ?

Posted 21 April 2013 - 03:55 PM

Say they were using PHP you could use my old tried and true method above and just periodically check for updates for idler users user AJAX JQUERY and if the timestamp was newer you add the article to the articles to the page but that's what I'm tryin to get across here is instead of doing this is using a simple PUSH like we are talking about A.) USABLE FOR THIS INSTEAD and B.) Is It practicle i.e. better

This post has been edited by Dormilich: 21 April 2013 - 11:06 PM

Was This Post Helpful? 0
  • +
  • -

#8 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Creating Real Time Web Updates With AJAX PHP SQL Etc. ?

Posted 21 April 2013 - 04:47 PM

Is it usable for this? Sure, it can be used in that situation. - Is it practical/better than the traditional AJAX timer approach? That depends on your code.

The upside to the sockets-like methods is that your server won't have to be executing opening and closing connections all the time to deal with the incoming HTTP request for updates, and the SQL server won't have to be queried all the time. It just keeps active connections to all the clients and when an update is made available by any of them, it can send them immediately to all the other clients. It won't even have to query the database for the data, because it already has the data from the sending client.

To make sense of how a socket server would work, this is basically the thinking behind that concept:
(This is not working code, it just demonstrates the logic.)
<?php
// Assume this SocketServer is dealing with incomming
// Socket connections and making them available as
// "Client" objects through: getClients().
$server = new SocketServer();

while ($server->isRunning()) {
	// Go through each of the clients currently connected
	// to the server.
	foreach ($server->getClients() as $client) {
		// If the client has sent a new message to the server
		// receive it and push it down to all the other clients.
		// Then save it in the database.
		if ($client->hasNewData()) {
			$message = $client->receive();
			foreach ($server->getClients() as $recipient) {
				$recipient->send($message);
			}
			DB::get()->saveMessage($message);
		}
	}
	
	// Sleep for a few milliseconds, just so the process doesn't
	// freeze up the server or eat all it's resources.
	usleep(150000);
}


In this code, each client just sites idle and waits until one of the clients sends some data, then it's sent that data. There is no pining the server repeatedly for updates, and the database isn't even accessed in order to fetch the data. The only role the database plays in this code is to store the data, and, in theory (though it's not shown there), to give the client some data when it initially connects. - You could remove the database from this altogether without affecting the system, except that there would be no permanent storage of the messages.

Of course, as the number of active connections increases, the strain on this script will increase. It's going through each connection on a loop, after all, to check for incoming data. But even with a fairly long AJAX timer, this should be much more efficient, as well as much more responsive. (You'd have to set the AJAX timer up for 150ms to match the responsiveness of this code, and that would be murder on the HTTP server with more than a handful of clients.)
Was This Post Helpful? 0
  • +
  • -

#9 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

Re: Creating Real Time Web Updates With AJAX PHP SQL Etc. ?

Posted 21 April 2013 - 07:38 PM

Right that's what I figured. So You're saying in general with a lot of visitors it is way more efficient to use this method VS's just using AJAX and a timer. That makes sense. What if you don't care too much about immediate updates. You could just have an AJAX timer check every 30 seconds or even a minute or something?

This post has been edited by Dormilich: 21 April 2013 - 11:06 PM

Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,249
  • Joined: 08-June 10

Re: Creating Real Time Web Updates With AJAX PHP SQL Etc. ?

Posted 21 April 2013 - 11:09 PM

that would decrease the incoming requests from all the clients (though the general problem* remains), but still less efficient than the socket approach.



* - you do lots of "unnecessary" requests
Was This Post Helpful? 0
  • +
  • -

#11 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

Re: Creating Real Time Web Updates With AJAX PHP SQL Etc. ?

Posted 22 April 2013 - 01:48 AM

So what is a good approach to real-time updates then? Or is this something that generally doesn't matter? What should you do? What are the best options?

This post has been edited by Dormilich: 22 April 2013 - 02:43 AM

Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,249
  • Joined: 08-June 10

Re: Creating Real Time Web Updates With AJAX PHP SQL Etc. ?

Posted 22 April 2013 - 02:47 AM

I still recommend the use of sockets.

personally, I would use node.js simply because it is an event driven server-side language (but any other event driven server-side language would do equally well).
Was This Post Helpful? 0
  • +
  • -

#13 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

Re: Creating Real Time Web Updates With AJAX PHP SQL Etc. ?

Posted 23 April 2013 - 07:44 PM

IS this a feature that really matters much though? I notice a lot of websites even major news sites that DO NOT use real time updates like this?

This post has been edited by Dormilich: 23 April 2013 - 11:14 PM

Was This Post Helpful? 0
  • +
  • -

#14 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Creating Real Time Web Updates With AJAX PHP SQL Etc. ?

Posted 23 April 2013 - 08:07 PM

They no doubt would if they could. After all, being able to quickly "push" news updates down to users without excessive overhead would be valuable to sites like that.

But major news sites need to be able to reach the widest audience possible, and both WebSockets and any sort of AJAX "push" method is limited in it's availability on browsers. Using a AJAX timer is the safer choice in most cases, which is why major sites use that method. Creating a system that implements both - uses sockets on new browsers and falls back on AJAX for old browsers - takes more time and is more complex, and thus costs more, which is usually why companies opt to just use the simplest, most commonly available option.
Was This Post Helpful? 0
  • +
  • -

#15 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

Re: Creating Real Time Web Updates With AJAX PHP SQL Etc. ?

Posted 23 April 2013 - 08:12 PM

I agree but I can also think of plenty of sites that don't use an AJAX timer with a database check or a sockets push of new information. I'm thinking craigslist for one that correct me if I'm wrong uses no real-time update methods.

I feel that most users these days have got it in their heads that if they want to see the latest on something craigslist or news etc. they just press REFRESH once and bam there you go.

I know this depends on the situation in terms of whether or not it's a good ideas to use but still you know?

This post has been edited by Dormilich: 23 April 2013 - 11:15 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2