[wp-hackers] Long Polling, Ajax and WordPress

William P. Davis will.davis at gmail.com
Tue Nov 1 12:10:55 UTC 2011


There are large sites, such as NY Times, that as far as I know use long polling, so it can be sustainable on large sites. I might consider using varnish to cache the result and then dumping the varnish cache when there are updates, though depending on the amount of data you are working with this may or may not be feasible. 

Will
Sent from my BlackBerry® wireless device

-----Original Message-----
From: Eric Mann <eric at eam.me>
Sender: wp-hackers-bounces at lists.automattic.com
Date: Mon, 31 Oct 2011 07:35:29 
To: <wp-hackers at lists.automattic.com>
Reply-To: wp-hackers at lists.automattic.com
Subject: Re: [wp-hackers] Long Polling, Ajax and WordPress

Alex,

If you're using a typical WordPress installation (i.e. shared host or no
access to the server environment), long polling is probably the best you
can accomplish.  Read up on the thread we we covered last month regarding a
similar situation - I wanted to use WebSockets to stream WP data to the
front end (
http://lists.automattic.com/pipermail/wp-hackers/2011-September/040806.html)

The system I'm working on now, and hope to demo in a couple of weeks, is
still built on WordPress, but uses some additional technologies to make
websockets possible.  Firstly, I switched my server environment from Apache
to Nginx so I could more effectively handle events without blocking the
current request thread.  I'm also building a custom Node.js application to
sit alongside my WordPress installation and serve up data.  This way,
websocket requests will be handled by the separate Node application, which
will be tied in to post publication/update hooks on the server end.

On Mon, Oct 31, 2011 at 7:28 AM, Dagan Henderson <
Dagan.Henderson at epyllion.com> wrote:

> Alex, if you want efficiency, you'll really need to be able to control the
> server environment. Is this a possibility?
>
>
> -----Original Message-----
> From: wp-hackers-bounces at lists.automattic.com [mailto:
> wp-hackers-bounces at lists.automattic.com] On Behalf Of Alex Hempton-Smith
> Sent: Monday, October 31, 2011 7:23 AM
> To: wp-hackers at lists.automattic.com
> Subject: [wp-hackers] Long Polling, Ajax and WordPress
>
> Hi all,
>
> The project I'm working on requires an 'activity stream' of WordPress
> posts to be updated live, via Ajax.
>
> I've read a lot about long polling, Comet etc. but I've found it hard to
> replicate. The code I'm using now essentially polls WordPress every 10
> seconds to check if there are new posts, and then appends them to the top
> of a list. This can't be very efficient, as most of the time the response
> from the server will be null.
>
> How have people achieved long-polling, or any other more efficient method
> to create live streams with WP?
>
> For reference, I have posted the code I'm using below. It's pretty simple
> (but any improvements to what I have are welcomed!)
>
> Cheers,
> Alex
>
>
> ==== Frontend Markup ====
>
> <ul class="activity-stream">
> <li data-timestamp="1320069292">Some post</li> <li
> data-timestamp="1320051123">Another post</li> </ul>
>
> ==== Javascript ====
>
> var startPolling = function() {
>
> var timestamp = jQuery('.activity-stream
> li:first-child').attr('data-timestamp');
>  var data = {
> action: 'my_ajax_update_activity_stream',
> latest_timestamp: timestamp
>  };
>  jQuery.post(
>  ajaxurl,
> data,
> function(response) {
>  jQuery('.activity-stream').prepend(response);
> setTimeout('startPolling()', 10000);
>  });
>
> };
>
> jQuery(document).ready(function(){
> startPolling();
> });
>
> ==== functions.php ====
>
>
> function filter_where( $where = '', $timestamp ) {
>
> // Check for posts after the latest timestamp  $where .= " AND post_date >
> '" . date('Y-m-d H:i:s', $timestamp) . "'"; return $where;
>
> }
>
> function my_ajax_update_activity_stream() {
>
> add_filter('posts_where', function($where) { return filter_where($where,
> $_POST['latest_timestamp']); });
>
> query_posts( 'post_type=myposttype&posts_per_page=-1' ); if ( have_posts()
> ):
>  while (have_posts()) : the_post();
> ?>
> <li data-timestamp="<?php echo get_the_time('U') ?>"><?php the_content();
> ?></li> <?php  endwhile; endif;
>
> remove_filter( 'posts_where', 'filter_where' );
>
> exit();
>
> }
> add_action('wp_ajax_my_ajax_update_activity_stream',
> 'my_ajax_update_activity_stream');
>
>
> ==== Ends ====
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
wp-hackers at lists.automattic.com
http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list