سؤال

Okay, here is my situation: I'm looking for the best design approach. I'm working in PHP/Smarty on the server-side and HTML/jQuery for the client-side, but that is not really important here.

I have this multi-user system on my server. It is a kind of ordering system. A standard user -- let's call them customer -- can order some items from the webshop. The webshop contains items from multiple sellers.

When the user (customer) places the order, the seller -- also a user in the system -- has to get notified that there is a new order and confirm/decline it.

When the seller confirms/declines the order, a notification has to be send to the user giving him/her the status of his/her order.

Orders are stored in the database, as well as order confirmations.

The only way I can think of right now is to constantly -- in short intervals with AJAX -- check for new records in the database, from the sellers' screen, and do the same thing for the customer when he/she is waiting for the confirmation.

But I'm thinking, is there any way to trigger notifications to the seller when the user(customer) places the order so the seller could load the database only when needed and not constantly in intervals?

The same goes for the customer when he/she is waiting for confirmation. But that is not so important because it doesn't happen all the time. There is a waiting limit when the order will be automatically declined if the seller doesn't respond.

I hope you understand my question.

هل كانت مفيدة؟

المحلول

The answer to a similar question here on Using Comet with PHP suggests that there could be some problems with Apache threads being tied up from all the open connections to the client.

According to this blog post on PHP Continuations, it is possible to use continuations with PHP, but there just doesn't seem to be as much documentation on the subject. However, CometChat did it in PHP. It's not clear if they are using continuations, but they claim to scale to 100,000 connections. More information on PHP Comet can be found in this similar Stack Overflow Question regarding A Solution for Comet and PHP.

I was also going to suggest using Java, as Java has a really great track record for implementing scalable Comet with Continuations. Conversion Support is an example of chat software that uses comet and continuations on a Jetty Web Server.

Since your code is in PHP, you could Use Querces to run your PHP code on the JVM. Additionally, Querces PHP Benchmarks Suggest it's Faster than Apache, which gives you added advantages. Check out the Querces Project for more information.

UPDATE: I suggest you do your own benchmarks or research the speed issue yourself, as there may be some information that may suggest Apache is faster. If using Querces, it's important to understand that you would essentially be writing Java that just happens to look and feel like PHP. Thus, I'd suggest additional research there as well so that you understand the advantages and disadvantages of this approach.

نصائح أخرى

Although instant updates would be nice, in reality, updates would never be instant anyway, there will always be a degree of latency inherent in transfering data over the internet.

The polling option seems more attractive for several reasons.

The system you describe sounds as if although it may start out small scale, it would easily scale to a multi-server configuration. Polling would allow you to create a polling server which the AJAX requests can query. This can be optimized for the small and fast nature of AJAX, where as the standard web server can be dedicated to web page display in the usual sense.

The polling idea also lends itself to a REST style API, making the polling area completely independant of the browser. You could find that further down the line, sellers on your system would prefer to have a native app, or even an iPhone/Android app. The REST API would allow you to do this from any app that can make an HTTP request.

Essentially you're avoiding being locked in to a particular technology, and that opens up future possibilties. It doesn't add any great amount of work, and provides a level of flexability that a permenent connection would not allow.

COMET and long polling as mentioned above are common solutions to this problem. But you could also look into HTML5 web sockets. It's supported in all browsers but there are patches/polyfills for IE browsers.

You could also look at Node.js to run along side apache.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top