Tech Support Guy banner
Status
Not open for further replies.

Solved: Creating a Real Time Application in PHP

1 reading
1.3K views 4 replies 2 participants last post by  JiminSA  
#1 ·
I am currently working on a php based Live Auction site and would like opinions on the best approach to use for seamless simultaneous interaction with unlimited users.
I have looked at Websockets and Ratchet, but would love to hear your advice ...:)
 
Save
#2 ·
Well I've found an alternative solution and would be pleased to hear some constructive criticism about my methodology ...
I am using this magical bit of Javascript to refresh a marquee and a bidding button to simulate streaming ...
Code:
	<script>
	var refreshId = setInterval(function()
	{
		 $('#marquee').fadeOut("slow").load('marquee.php').fadeIn("fast");
	}, 5000);
	</script>
	
	<script>
	var refreshId = setInterval(function()
	{
		 $('#bid_button').fadeOut("slow").load('button.php').fadeIn("fast");
	}, 5000);
	</script>
and I utilise $_SESSION variables within the refreshed divs (content held in the "called" php pages) to update marquee and bid button content to reflect the current bid and bidder and the next bid respectively.
The code rolls like this ...
Live Auction page
HTML:
[IMG alt="loader"]images/loading.gif[/IMG]


							">
							">
marquee.php
PHP:
<?php

	session_start();

	date_default_timezone_set('Africa/Johannesburg');

	include ('functions.php');

	OpenDatabase();

	GetBid();
	$temp_row = mysql_fetch_array($temp_result);
	$_SESSION['bidder'] = $temp_row['bidder'];
	$_SESSION['curr_bid'] = $temp_row['latest'];

	$nb_num = trim($_SESSION['curr_bid'], "R");
	$nb_num = $nb_num + $_SESSION['factor'];
	$_SESSION['next_bid'] = "R" . $nb_num;

?>

[B][SIZE=15]Current Bid [I]<?php echo $_SESSION['curr_bid']; ?>[/I] from <?php echo $_SESSION['bidder']; ?> ...[/SIZE][/B]
button.php
PHP:
<?php

	session_start();

	date_default_timezone_set('Africa/Johannesburg');

	include ('functions.php');

	OpenDatabase();

	GetBid();

	$temp_row = mysql_fetch_array($temp_result);

	$_SESSION['curr_bid'] = $temp_row['latest'];

	$nb_num = trim($_SESSION['curr_bid'], "R");
	$nb_num = $nb_num + $_SESSION['factor'];

	$_SESSION['next_bid'] = "R" . $nb_num;
?>
								?"</input>
 
Save
#3 ·
Woops! forgot to mention that I put the $_SESSION variables in a db to spread across logged in users ...
 
Save
#4 ·
That feels like the wrong way to do it, though I don't know what I'd suggest instead.

The first problem, which I suppose is efficiency rather than anything else, is that every time you check you load the whole page. It would be better to first find out whether anything has changed before you go through the rigmarole of replacing the page.

The other thing is that it's inherently a polling system, which means as well as constantly checking when nothing has changed, you don't find out immediately once something does.
I don't know whether any of the current approaches to a push notifications system work terribly well, but if any of them is available they'd be preferable. (I guess you could fall back to a polling system for unsupported clients.)
 
Save
#5 ·
... every time you check you load the whole page
Not quite so Josiah, just 2 small divs are reloaded every 10 seconds not the whole page.
 
Save
Status
Not open for further replies.
You have insufficient privileges to reply here.