21.07.2015 Views

Linux Journal | January 2013 | Issue 225 - ACM Digital Library

Linux Journal | January 2013 | Issue 225 - ACM Digital Library

Linux Journal | January 2013 | Issue 225 - ACM Digital Library

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

COLUMNSAT THE FORGEsmall Ruby program, update-stocks.rb,which is shown in Listing 2. Thisprogram uses the “pusher” gem,provided free of charge by the Pusherpeople. You then choose one of thecompanies in your list (the constantarray COMPANIES), then choose arandom number up to 100. Next,you send the message to all of thesubscribers on the “stock_ticker”channel, indicating that you’ve sentan “update_event”. Because of thedecoupled nature of communicationbetween publisher and subscriber,you won’t get an error message if youmisspell the channel or event name.Rather, the message will be deliveredto no one. Thus, you will want to beparticularly careful when writing theseand ensure that the same names areused in your client and your server.ConclusionWeb sockets are going to change theWeb dramatically, but it’s not yet clearhow or when. Being able to update alarge number of client displays almostsimultaneously using pub-sub is alreadychanging the way people see Webapps—and as you can see from this smallexample application, it isn’t very difficultto do. Pub-sub isn’t appropriate forall applications, but if you are sendingthe same data to many people, andif they might want to receive updatesautomatically into their browsers, this is aneasy and straightforward way to do it.■Reuven M. Lerner is a longtime Web developer, consultantand trainer. He is also finishing a PhD in learning sciences atNorthwestern University. His latest project, SaveMyWebApp.com,went live this spring. Reuven lives with his wife and childrenin Modi’in, Israel. You can reach him at reuven@lerner.co.il.ResourcesYou can learn about Web sockets from a variety of sources. The W3C’s API and definitionare at http://www.w3.org/TR/2009/WD-websockets-20090423, in a document thatis surprisingly readable. Another good source of information is the book ProgrammingHTML5 Applications written by my colleague Zach Kessin and published by O’Reilly.Web socket servers have been written in nearly every language you can imagine. I founda relatively up-to-date list, with links, on Wikipedia, under the “Web socket” entry, andthus, I’ll not try to reproduce it here.You can learn more about Pusher at http://pusher.com or a popular competitor, PubNub,at http://pubnub.com.WWW.LINUXJOURNAL.COM / JANUARY <strong>2013</strong> / 43

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!