[wp-hackers] WordPress and Websockets

Dougal Campbell dougal at gunters.org
Thu Sep 22 22:11:26 UTC 2011

On Sep 22 2011 3:34 PM, Eric Mann wrote:
> On Thu, Sep 22, 2011 at 11:54 AM, Dougal Campbell<dougal at gunters.org>wrote:
>> Have you looked at how the P2 theme does its dynamic front-page updates
>> when new posts and comments are posted? You might be able to borrow code
>> from there without too much fuss.
> I have, and I plan to use that style of polling as a fallback, specifically
> for browsers that don't support websockets (Internet Explorer 9 and below,
> Firefox 4, etc). But as Braydon pointed out, polling isn't quite real-time
> data exchange.

Yeah, I wasn't sure how real-time you really needed it to be.

> One option would be to enable websockets on the server if the server is
> capable of using them and defaulting to AJAX long polling if it isn't.  If
> that's the direction, what's the easiest way to detect *server* support for
> the technology?  Browser support is easy to detect using Modernizr ...

I've done some socket.io stuff with a node.js server, but I haven't 
looked into the details of the websockets protocol before. The protocol 
definition is here:


And looking at Section 4.1, some of the criteria for the server response 

   * Must return a 101 response code (or it could be a 401 auth request, 
or perhaps a redirect, that eventually reroute to a 101)
   * Must include a header of  "Upgrade: websocket"
   * Must include a "Connection" header with an appropriate token value
   * Must include a "Sec-WebSocket-Accept" header with an appropriate 
calculated key

There are other details, but that's the core of the websocket response.

If this is something you're planning to deploy in the wild (i.e., 
arbitrary servers that you don't know the details of), websocket support 
on servers is going to be pretty rare. In my case, I was thinking about 
proxying websocket requests to node through an nginx front-end. But 
nginx doesn't support ws out-of-the-box either, so I'd need to install a 
third-party extension.

Dougal Campbell <dougal at gunters.org>

More information about the wp-hackers mailing list