I recently came across SocketStream, which describes itself as “A fast, modular Node.js web framework dedicated to building single-page realtime apps”.
And indeed, it took virtually no effort to get this self-updating page in a web browser:
The input comes from the serial port, I just added this code:
That’s not JavaScript, but CoffeeScript – a dialect with a concise functional notation (and significant white-space indentation), which gets turned into JavaScript on the fly.
The above does a lot more than collect serial data: the “try” block converts the text to a binary buffer in the form of a JavaScript DataView, ready for decoding and then publishes each packet on its corresponding channel. Just to try out some ideas…
I’m also using Jade here, a notation which gets transformed into HTML – on the fly:
And this is Stylus, a shorthand notation which generates CSS (yep, again on the fly):
All of these are completely gone once development is over: with one command, you generate a complete app which contains only pure JavaScript, HTML, and CSS files.
I’m slowly falling in love with all these notations – yeah, I know, very unprofessional!
Apart from installing SocketStream using “npm install -g SocketStream”, adding the SerialPort module to the dependencies, and scratching my head for a few hours to figure out how all this machinery works, that is virtually all I had to do.
Development is blindingly fast when it comes to client side editing: just save any file and the browser(s) will automatically reload. With a text editor that saves changes on focus loss, the process becomes instant: edit and switch to the browser. Boom – updated!
Server-side changes require nodemon (as described in the faq). After that, server reload becomes equally automatic during development. Pretty amazing stuff.
The trade-off here is learning to understand these libraries and tools and playing by their rules, versus having to write a lot more yourself. But from what I’ve seen so far, SocketStream with Express, CoffeeScript, Jade, Stylus, SocketIO, Node.js, SerialPort, Redis, etc. take a staggering amount of work off my shoulders – all 100% open source.
There’s a pubsub-over-WebSockets mechanism inside SocketStream. Using Redis.
Wow. Creating responsive real-time apps hasn’t been this much fun in a long time!
You might also want to check out Meteor (http://meteor.com). It can make real-time stuff a joy.
Thanks – yes, I did look into Meteor a bit. I found SocketStream somewhat easier to get started, and giving me more options to try out different package combinations. Both get client and server tied together in a very nice way – see also this informative weblog post by Steven Sanderson about several options (including Meteor but not SocketStream).
Still reading daily, and I like where this is going! (Another pointer http://square.github.com/cubism/) …
Thanks for the pointer. I had seen D3 before, but not the Cubism system built on top. D3 looks like a very interesting option for graphing. Amazing interactive demo’s.
See also Rickshaw – again built on top of D3 – example.
There are tons of chart packages for JavaScript: http://sixrevisions.com/javascript/20-fresh-javascript-data-visualization-libraries/ – e.g. dygraphs.
Sure, tons (-, http://selection.datavisualization.ch/
Wow, thanks – great overview!
Any suggested reading list? — I suppose one needs a basic understanding of Javascript before looking at Node? I may have some time for reading during Christmas or New Years. thanks!
Good idea. I’ll see if I can come up with a list in an upcoming weblog post.