Author

Topic: Web language question (Read 1012 times)

legendary
Activity: 1750
Merit: 1007
November 27, 2011, 02:18:15 AM
#7
If you want live updating for OTHER users, this can be accomplished using long polling (yes, just like Bitcoin Mining).  I've done it before for a web RPG where it had a true live chat and map where players walked around (scrolling like classic console RPGs).

There's two ways to pull it off without newer technologies like web sockets:

1) Bitcoin style longpolling, where an AJAX request is directed to longpoll.php .  This is a looping PHP script (or if you want higher efficiency, a custom made TCP socket program that responds with HTTP formatted responses), that consistently checks for a modified variable.  You could do this either with a file on the webserver that simply stores an incrementing counter each time something changes, or a temporary table in your database that increments a counter on each change.

When the counter increments, the AJAX query complete, and you set your response function to issue a new request for the updated page, and then initialize a new long poll connection.

2) A bit trickier, this involves the same looping script/TCP socket program.  However, instead of firing off a simple "data updated" response and then having your javascript issue a new request, it streams the response back.  The hard part in this is the way different browsers handle XmlHttpRequest objects when the response is only partial.  For firefox I believe it required 1 kB of data sent back before it would allow you to reference the current response text.  In internet explorer, its even more difficult and requires some very ugly "virtual iframes" (invisible iframe objects that aren't actually a part of the page document).  Your PHP script will have to force output as its ready for parsing using ob_start/ob_flush, etc.

The advantage to this method is you achieve a true "live" system other than the minor sleep at the end of the PHP loop, which can be as low or high as you want depending on server specs.



For the 'who pressed first' scenario, obviously it's just a matter of forcing a timestamp being posted as part of the script that executes when a button is pressed.  The server generates/stores the timestamp, so there is no way for a tie/modified timestamp.


Since you're only talking about a few hundred online simultaneously, you're not going to run into too many problems unless you're in a shared hosting or budget VPS provider scenario.
sr. member
Activity: 322
Merit: 251
FirstBits: 168Bc
November 24, 2011, 02:34:11 PM
#6
I mention Redis because it takes very simple HTTP REST requests, all single threaded-atomic, and I don't believe there is anything faster. Clients could easily poll the redis servers. Read-only replicated Redis slaves could reduce the burden of the write-master instance, but I doubt you'd need that with only 10K clients. If you don't need server side logic, why introduce an unnecessary bottleneck? -- unless you don't trust the clients.
full member
Activity: 154
Merit: 102
Bitcoin!
November 24, 2011, 02:28:23 PM
#5
Trying to figure if javascript and php is good enough for an idea I have, and if not, what language(s) I need.

A player clicks something which modifies the database, immediately the values from the database that were showing on the page are updated, no page refresh needed.

Another player who is currently viewing the same values on their page also needs to be instantly updated.

What is it best and will scale well? Database of ~10k players, several hundred logged in, ~50 columns if any of that matters.

And what about handling a "who pressed first" scenario? I don't really care if it is super accurate, just that it can pick one winner. I think this part is trivial, just want verification.

Didn't want to bog down with details, let me know if more is needed.
You can easily do all of that with Javascript to make an AJAX call to the back end PHP script to update the database.  The who pressed first scenario is easy in that the first one to update the database wins.  The only iffy part is updating all other players' pages.

Another player who is currently viewing the same values on their page also needs to be instantly updated.
Being stateless, the HTTP can't handle push notifications from server to client.  The way this is typically handled in Javascript is to set a timeout and poll the server for updated values at a given interval.  If things don't have to be instantaneous, this works well.  If you want instant or near instant updates, then you would have to poll the server for updated information every second or every 500 ms or so.  If you have lots of online users, this can be a bottleneck unless you beef up your web server and database server to handle all the poll requests.
sr. member
Activity: 322
Merit: 251
FirstBits: 168Bc
November 24, 2011, 02:20:11 PM
#4
Is this really about languages or databases? Have you looked into Redis? Atomic, fast as lightning. At least when I shard'd 200K transactions per second for a project 9 months ago.
hero member
Activity: 784
Merit: 1000
bitcoin hundred-aire
November 24, 2011, 01:39:04 PM
#3
Update procedure: user data -> jQuery -> update.php -> DB -> immediate update by jQuery -> user view
For everyone else: user <- jQuery timer request <- view.php <- DB
sr. member
Activity: 262
Merit: 250
November 24, 2011, 01:34:10 PM
#2
You could do this with PHP and Javascript.

Have a look at JQuery if you haven't already and the Ajax and timer methods.

legendary
Activity: 1246
Merit: 1016
Strength in numbers
November 24, 2011, 06:19:17 AM
#1
Trying to figure if javascript and php is good enough for an idea I have, and if not, what language(s) I need.

A player clicks something which modifies the database, immediately the values from the database that were showing on the page are updated, no page refresh needed.

Another player who is currently viewing the same values on their page also needs to be instantly updated.

What is it best and will scale well? Database of ~10k players, several hundred logged in, ~50 columns if any of that matters.

And what about handling a "who pressed first" scenario? I don't really care if it is super accurate, just that it can pick one winner. I think this part is trivial, just want verification.

Didn't want to bog down with details, let me know if more is needed.
Jump to: