Erwin Moller wrote:
> On 6/10/2015 3:59 PM,
apoorv....@gmail.com wrote:
>> I want to get response from server every one second, the output will
>> contain some value which I need to process in front end.
>>
>> In backend I am using some queries to read data from mysql and return me
>> output.
>>
>> Things I have tried 1) Ajax 2) Webserver Events 3) Amazon Cloudfront
>>
>> None of these work for me usually it is taking 3-4 seconds to get me
>> response,
>>
>> classic example to see this working(getting output in one sec)
>>
>>
http://madbid.com
>
> […]
> 1) How many clients are requesting that info on the server?
> If you have multiple clients, think over your design (i'll come back to
> that)
No. See below.
> Let's look at XML HTTP Request (XHR) first (what you called AJAX):
> What happens?
> a) Client request a PHP page
No, that is precisely what in most cases does _not_ happen with XHR.
Which is why it is fast as compared to traditional approaches.
> b) Server much launch the process
> c) PHP runs, and starts a database query
> d) Database processes the query, and answers
> e) PHP delivers the answer as output back to the requesting client
> f) Client updates the screen
>
> This is just not feasible every second.
Probably true. Which is why you do not do low-latency work this way.
ad a) You do not request an entire document, only the data;
the HTTP connection is still open from before;
ad b) the server does not need to launch the PHP process because that
is still running from before: use PHP via FastCGI, not as Apache
module;
ad c) the database query result is cached by the database server,
so that the need for subsequent queries is minimized;
ad d) the query is optimized so as to run fast, by using indexes
and so on.
ad e) PHP delivers only the data to the Web server, or *as* Web
*service*, which serves it to the Web client;
ad f) Client-side scripting uses the data from the response
to update only parts of the document.
> Try that with 1000 clients. ;-)
No problem. Web server software can run multiple instances of itself and
each instance can run multiple threads. It is merely a matter of hardware
performance. BTST where I am working.
> There is so much overhead involved, and networking, you can't expect
> this to run every second.
True. But see above.
> Solution:
> Create a cronjob that runs every second.
> Let it call your PHP script that queries the database.
> Instead of responding to a client, let PHP put its output in a file in a
> public directory.
> Let XHR directly fetch this file.
> This is much faster.
And the data written will be out of date, and there will be concurrency
problems, while this is heavy on the hardware. Do not do that unless you
want to wreck your harddisk (or memory, if you use in-memory storage) in
the long-term.
> Warning: write the file as myOutput_tmp.xml, then rename to
> myOutput.xml, otherwise you risk trying to read a file that is in the
> process of being written.
Even heavier on resources. How many copies do you suggest should be lying
around on the harddisk? This is an Incredibly Bad Idea[tm].
Also, XML is not a good format for database dumps as it has much overhead.
JSON or YAML leaner formats, in general better suited to the task. But not
in this case.
The idea behind this is a good one though if the database is not updated
every second. The idea is called caching. You would not do that in the
filesystem, though, at least not directly. Server-side sessions come to
mind.
> 2) Use nodeJS
> Indeed, no PHP. No AJAX.
> NodeJS is extremely fast and supports sockets.
So is and does PHP. You are comparing apples and oranges. PHP does not
have to run via a Web server like Apache; you can write a Web service in
PHP, much like a nodeJS application.
> If you are a bit familiar with ECMA Script (such as Javascript), try
> this route.
_ECMAScript_, _JavaScript_. JavaScript is an implementation of ECMAScript;
actually JavaScript *are* implementations of ECMAScript as there are several
implementations (but not all of them) that contain “JavaScript” in their
name. At the time of writing, Netscape/Mozilla JavaScript, Google V8
JavaScript (in Chromium and nodeJS, now in Opera too), and KDE JavaScript,
to name a few. But there is also Microsoft JScript and Opera ECMAScript (in
older versions).
However, it is not so much a matter of the programming language but of the
browser environment API (e.g. the DOM), although whatever ECMAScript
implementation the HTML user agent supports is now [HTML5] the standard
default client-side scripting language, and implicitly has been the quasi-
standard for years.
<
http://PointedEars.de/es-matrix>
--
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.