A tale of WebSockets and JavaScript
Friday, November 13, 2015 by Dave Winer

I had a problem with the liveblog that was solved by WebSockets.

At first I was using longpolling to handle updates. Basically each time you load Scripting News, it connects with the 1999.io server with a request that doesn't return until there's an update to the content on Scripting News, or it times out.

I did it this way because I had lots of experience with longpolling, and had code that worked. But it had a problem.

If I had two tabs open in Chrome and both were waiting for a longpoll to return from the same server, the browser would get confused and only one would get the response. So to get the update you'd have to reload the page. Which defeats the purpose of having a live connection.

I wondered if WebSockets would work better. 

So I implemented WebSockets support on the server, using the nodejs-websocket package, and hooked up a test app to the server, and launched three instances in separate tabs in a single Chrome window. And I held my breath.

It worked! I suspected the browser guys might have tested this case, and the bet paid off. So I adapted the Scripting News home page app to use WebSockets, and all is good. 

I celebrated in a tweet last night when I got this working. wink

As they say.. 

Still diggin!