| | |
Most efficient "real-time" system. Socket Server or Client Queries
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Aug 2009
Posts: 36
Reputation:
Solved Threads: 0
Hi everyone, I am trying to create a system that will allow me to update the browsers of each client (person) currently viewing a webpage.
Don't worry so much about why I am wanting to do this, but please critique my 2 solutions, and tell me which one is the most efficient (least amount of server strain) and can provide the fastest response times (lower latency, best simulation of "real time" updates)
Here are my 2 ideas, help me determine which is best please.
Can't insert an image, so please view this link to see the image!
http://neverlettinggo.com/design%20ideas.png
Don't worry so much about why I am wanting to do this, but please critique my 2 solutions, and tell me which one is the most efficient (least amount of server strain) and can provide the fastest response times (lower latency, best simulation of "real time" updates)
Here are my 2 ideas, help me determine which is best please.
Can't insert an image, so please view this link to see the image!
http://neverlettinggo.com/design%20ideas.png
•
•
•
•
Hi everyone, I am trying to create a system that will allow me to update the browsers of each client (person) currently viewing a webpage.
Don't worry so much about why I am wanting to do this, but please critique my 2 solutions, and tell me which one is the most efficient (least amount of server strain) and can provide the fastest response times (lower latency, best simulation of "real time" updates)
Here are my 2 ideas, help me determine which is best please.
Can't insert an image, so please view this link to see the image!
http://neverlettinggo.com/design%20ideas.png
Having the server push data to the clients is the best way to go. So a socket deamon would be more suited. However, you can also get HTTP to push data if you use a server that supports it. Usually this would be specialized servers, or specialized modules for HTTP servers. Usually the HTTP push is called Comet. If you are using a browser plugin such as Flash, then you should just go with TCP since you have the support already.
If you'll be handling a lot of users it probably is a better choice to use an existing game server, or IM server. I don't know any game servers, but an XMPP server such as EJabberd (Elang) or OpenFire (Java) can handle a hundred thousand or more TCP connections on one machine. You would just need to write a module to handle your game protocol, which you can define in XML.
An example of a game run over XMPP is http://www.chesspark.com/
Also look into Red5:
http://osflash.org/red5
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
•
•
Join Date: Aug 2009
Posts: 36
Reputation:
Solved Threads: 0
Thanks, yes I will be using Flash, and for games UDP will be better than TCP, so..
Anyways, I dont understand why you think Option 1 will be better than option 2. Option 1 has to, in an infinite loop, query the database for changes AND send these changes to ALL clients (until if-statements prevent the server from sending changes to SOME clients unnecessarily.
Why is that a better option than just clients querying a database on their own, without the need for a server?
And yes, I am well versed in Comet Programming.
Anyways, I dont understand why you think Option 1 will be better than option 2. Option 1 has to, in an infinite loop, query the database for changes AND send these changes to ALL clients (until if-statements prevent the server from sending changes to SOME clients unnecessarily.
Why is that a better option than just clients querying a database on their own, without the need for a server?
And yes, I am well versed in Comet Programming.
•
•
•
•
Thanks, yes I will be using Flash, and for games UDP will be better than TCP, so..
Anyways, I dont understand why you think Option 1 will be better than option 2. Option 1 has to, in an infinite loop, query the database for changes AND send these changes to ALL clients (until if-statements prevent the server from sending changes to SOME clients unnecessarily.
Why is that a better option than just clients querying a database on their own, without the need for a server?
And yes, I am well versed in Comet Programming.
Usually you'd need an interface there to enforce access control to the database data.
In any case, letting the clients query the database directly will still be more inefficient then limiting the processes that query the database.
If you have 100 clients. The worst case is 100 concurrent queries to the DB in 2. However, in case 1, you only have 1 query for all 100 clients at a time. (keeping things linear here for simplicity)
Another way of putting it. Say your changes are in a table called "events".
In case 2 you have:
PHP Syntax (Toggle Plain Text)
select * from events where id > $last_event_id where client_id = $myid;
You them multiply this by 100 concurrent queries.
For case 1 you have:
PHP Syntax (Toggle Plain Text)
select * from events where id > $last_event_id; $result = get_db_result() foreach($result as $row) dispatch_event_to_client($row->client_id, $row);
Where dispatch_event_to_client() could be spawned or passed to a new process so it doesn't block waiting on network latency.
For the case 2, you have 100 queries waiting on external network latency, compared to 1 query over a local socket.
Usually, if you can avoid the database for event notification, you should. Keep the events in memory and broadcast to the clients. Copy to the database when no more clients need it from memory. That way there is no blocking on event notification, and you still keep the changes for later retrieval.
Last edited by digital-ether; Aug 16th, 2009 at 12:54 pm.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
•
•
Join Date: Aug 2009
Posts: 36
Reputation:
Solved Threads: 0
"Unless you fully trust your clients, and encrypt traffic, I don't see how you can let clients query the database directly, even if read only.
Usually you'd need an interface there to enforce access control to the database data."
You are looking at a top-level diagram, of course there will be an interface for client-database interaction.
And I think you missed something else as well. I have an idea I am trying to turn to life, which involves simulation of real time, which means the LOWEST amount of delay-per-response is absolutely essential to the system.
You say I can just query once and then dispatch, but you seem to have forgotten you need to dispatch to everyone. Which means +1 for server-to-database and +100 for server-to-client. Thats 101 as opposed to the 100 you said earlier.
Modern day servers are designed to get hit with queries constantly, so I don't think we'll have an issue there, but I just hope there isnt too much of a delay in response. When running, I can use some code to start before the query, and then stop after the response is received, and display the time it took per response to benchmark.
I like this option (option 2) because I don't know how to program sockets in PHP, only C++. If they are EASIER than C++ sockets, I think Option 1 would be the best option. (I should add I also don't know about spawning separate threads).
Tell ya what, I'm gonna spend a few hours reading PHP socket server/client tutorials, and try to come up with a basic server that can send a message out to 1 client, then all clients. That's a good start.
Digital-ether, keep it real buddy. Thanks for the help thus far!
Usually you'd need an interface there to enforce access control to the database data."
You are looking at a top-level diagram, of course there will be an interface for client-database interaction.
And I think you missed something else as well. I have an idea I am trying to turn to life, which involves simulation of real time, which means the LOWEST amount of delay-per-response is absolutely essential to the system.
You say I can just query once and then dispatch, but you seem to have forgotten you need to dispatch to everyone. Which means +1 for server-to-database and +100 for server-to-client. Thats 101 as opposed to the 100 you said earlier.
Modern day servers are designed to get hit with queries constantly, so I don't think we'll have an issue there, but I just hope there isnt too much of a delay in response. When running, I can use some code to start before the query, and then stop after the response is received, and display the time it took per response to benchmark.
I like this option (option 2) because I don't know how to program sockets in PHP, only C++. If they are EASIER than C++ sockets, I think Option 1 would be the best option. (I should add I also don't know about spawning separate threads).
Tell ya what, I'm gonna spend a few hours reading PHP socket server/client tutorials, and try to come up with a basic server that can send a message out to 1 client, then all clients. That's a good start.
Digital-ether, keep it real buddy. Thanks for the help thus far!
•
•
•
•
And I think you missed something else as well. I have an idea I am trying to turn to life, which involves simulation of real time, which means the LOWEST amount of delay-per-response is absolutely essential to the system.
You say I can just query once and then dispatch, but you seem to have forgotten you need to dispatch to everyone. Which means +1 for server-to-database and +100 for server-to-client. Thats 101 as opposed to the 100 you said earlier.
select * from events where id > $last_event_id;
$result = get_db_result()
foreach($result as $row)
dispatch_event_to_client($row->client_id, $row);
It is the "dispatch_event_to_client()" function in this case.
It isn't the sending of data to client that I was talking about, that is impossible to remove from the system. The client has to know that an event happened.
What you can remove is the requests. The client does not have to ask for this event. A better way to put it is for 2) you have 100 requests, and 100 responses.
For 1) you have one request, and 100 responses. But most of the time, you only have just one request happening. Note that when you need to send 100 responses, you do so in another process, or thread.
For 2) event if nothing happended, you still have 100 requests and responses.
•
•
•
•
Modern day servers are designed to get hit with queries constantly, so I don't think we'll have an issue there, but I just hope there isnt too much of a delay in response. When running, I can use some code to start before the query, and then stop after the response is received, and display the time it took per response to benchmark.
Web Servers are built of HTTP. HTTP is not built for sending lots of messages to small clients, take a look at how much twitter goes down. This is due to the large volume of small updates.
•
•
•
•
And I think you missed something else as well. I have an idea I am trying to turn to life, which involves simulation of real time, which means the LOWEST amount of delay-per-response is absolutely essential to the system.
•
•
•
•
I like this option (option 2) because I don't know how to program sockets in PHP, only C++. If they are EASIER than C++ sockets, I think Option 1 would be the best option. (I should add I also don't know about spawning separate threads).
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- "interactive refresh" ? (PHP)
- php socket server - can't connect from flash xml client socket (PHP)
- how to obtain the date and time of server from client by vb coding (Visual Basic 4 / 5 / 6)
- Real-time simulation problem (C++)
- Real-time OS (Computer Science)
- My yahoo client. Not receiving data in real time :(( Just need a bit of advice (Perl)
- When a host says "included scripts" (Networking Hardware Configuration)
- help to set up "Sun Java™ System Application Server PE 8" for web application (Java)
Other Threads in the PHP Forum
- Previous Thread: mysql/php
- Next Thread: please refine these codes for slideshow of images.....
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql number oop parameter paypal pdf php phpincludeissue problem query radio random recourse recursion regex remote script search seo server sessions sms source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white wordpress xml youtube






