Toying with Realtime Data and Web Sockets
Join the DZone community and get the full member experience.Join For Free
recently i was acting as a “second set of eyes” to help out fellow adobe evangelist kevin hoyt track down a quirk with a websockets example that he was putting together. kevin has a great writeup to familiarize yourself with web sockets & streaming communication that i highly recommend checking out.
while working with kevin’s code, i started tinkering… “what if i change this, what if i tweak that?” next thing you know, i put together a sample scenario showing subscription-based realtime data streaming to multiple web clients using web sockets. check out the video below to see it in action.
you are seeing 9 separate browser instances getting realtime push-based updates from a local server using web sockets. when the browser loads, the html-based client makes a web socket connection, then requests all symbols from the server. the server then sends the stock symbol definitions back to the client and displays them within the html user interface. from there, the user can click on a stock symbol to subscribe to updates for that particular symbol. disclaimer: all that data is randomly generated!
i put together this example for experimentation, but also to highlight a few technical scenarios for html-based applications. specifically:
- realtime/push data in html-based apps
- per-client subscriptions for realtime data
- multi-series realtime data visualization in html-based apps
one thing to keep in mind is that web sockets are not widely supported in all browsers yet. there is a great reference matrix for web socket support from caniuse.com :
if you still aren’t sure if your browser supports web sockets, you can also check simply by visiting websocketstest.com/ . if you want to test for web socket support within your own applications, you can easily check for support using modernizr . note: i didn’t add the modernizr test in this example… i only tested in chrome on osx.
ok, now back to the sample application. all of the source code for this example is available on github at: https://github.com/triceam/websocket-streaming-example . to run it yourself, you first have to launch the server. you can do this on the command line by invoking adl (part of the air sdk ):
cd "/applications/adobe flash builder 4.6/sdks/4.6.0/bin" ./adl ~/documents/dev/websocket-streaming-example/server/application.xml
you’ll know the server is started b/c an air window will popup (you can ignore this, just don’t close it), and you will start seeing feed updates in the console output.
once the server is running, open “ client/client.html ” in your browser. it will connect to the local server, and then request the list of symbols. if you click on a symbol, it will subscribe to that feed. just click on the symbol name again to unsubscribe. you’ll know the feed is subscribed b/c the symbol will show up in a color (matching the corresponding feed on the chart). again, let me reiterate that i only tested this in chrome.
you can open up numerous client instances, and all will receive the same updates in real time for each subscribed stock symbol.
the “meat” of code for the server starts in server/scripts/server/server.js . basically, the server loads a configuration file for the socket server, then creates a connectionmanager and datafeed (both of these are custom js classes). the connectionmanager class encapsulates all logic around socket connections. this includes managing the serversocket as well as all client socket instances and events. the datafeed class handles data within the app. first, it generates random data, then sets up an interval to generate random data updates. for every data update, the connectionmanager instance’s dispatch() method is invoked to send updates to all subscribed clients. rather than trying to put lots of code snippets inline in this post (which would just be more confusing), check out the full source at: https://github.com/triceam/websocket-streaming-example/tree/master/server
the client code all starts in client.html , with the application logic inside of client/scripts/client.js . once the client interface loads, it connects to the web socket and adds the appropriate event handlers. once subscribed to a data feed, realtime data will be returned via the web socket instance, transformed slightly to fit the data visualization structure, then rendered in an html canvas using the rgraph data visualization library . rgraph is free to get started with, however if you want to deploy a production app with it, you’ll need a license . you’ll notice that each feed updates independently, based upon the client subscriptions. note: the data visualization is not temporally aligned… if you want the updates in time-sequence, there is a litte bit more work involved in the client-side data transformation.
again, rather than trying to put lots of confusing code snippets inline in this post, check out the full client side source at: https://github.com/triceam/websocket-streaming-example/tree/master/client
this example is intended to get your minds rolling with the concepts; it is not *yet* an all-encompassing enterprise solution. you can expect to see a few more data push scenarios here in the near future, based on different enterprise server technologies.
Published at DZone with permission of Andrew Trice, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.