Charlie Harvey

Tech briefing: CSS book, Node.js and some other javascript tools

This is a writeup of my notes from my August reading day and might therefore be less coherent than my usual ramblings on the site. I read a book called Everything you know about CSS is wrong! and then moved on to some research on node.js which seems to be the thing that all the web hipsters are loving these days.

Everything you know about CSS is wrong!

This is actually rather an old book — hailing as it does from the days of IE8 — and one that I might have done better to read when it came out. It is from SitePoint, whose output I find quite hit and miss. But it has the advantage of being rather short and therefore readable in a few hours.

The book mostly talks about the then "new" features that CSS boasts, and is particularly excited about the prospect of being able to do grid layouts with display and the table layout types. Not layout with HTML tables. I found some of the info interesting and they had taken the trouble to quote some well respected designers, including Jeffrey Zeldman and Andy Clarke, who we worked with at New Internationalist. But I didn’t get much from it that I wasn’t already aware of.

Node.js

What is node.js?

Node.js, or just node (less typing) is a set of libraries that let you use Google’s v8 javascript engine to do networking. It is particularly suitable for DIRT applications. It runs an event loop in what is effectively the Reactor pattern. This means lots of callbacks and callbacks to callbacks and so on, which is the part of the programming model that is less well understood than other concurrency techniques, particularly threads.

It is quite a new technology, though it has been used in production in a few places. It uses reference counting to decide when to exit meaning that when there is nothing left to do it will quit. It is great for constructing network applications and i was able to build a chat server in about half an hour after following along with the presentation from Ryan Dahl who created node.

Example code

Here is the example code for a chat server. I added some features to the one that Ryan made. It can be run and handle letting all the people connected to it chatting and that sort of thing. It just keeps track of the open sockets in an array. You would use a better data structure in a real system.var net = require("net"); // all the sockets that are connected get pushed into this array var sockets = []; // this is the server log such as it is. That is only the admin can see it. console.log("Starting server ...") var s = net.Server(function(socket) { var joinMsg = "!! " + socket.remoteAddress + " Joins"; console.log(joinMsg); for(var i=0;i<sockets.length;i++) { sockets[i].write(joinMsg + '\n'); // we broadcast that new person joined } sockets.push(socket); // and add the new socket to the list socket.write("Welcome to the chat server.\nUse /q on a line by itself to leave.\n\n"); /// deal with the case when somebody disconnects by chopping them out // of the connected sockets list socket.on("end", function(d){ var i = sockets.indexOf(socket); sockets.splice(i,1); }); // and process incming data socket.on("data", function(d){ for(var i=0;i<sockets.length;i++) { // skip lines starting / and don't broadcast to ourselves if(d.toString().charAt(0) != '/' && sockets[i] != socket) sockets[i].write(socket.remoteAddress + ": " + d); } // /q quits if(d=="/q\n") { var leaveMsg = "!! " + socket.remoteAddress + " Leaves"; console.log(leaveMsg); socket.end(leaveMsg + '\n'); } }); }); s.listen(8000);

What is it useful for?

Very useful for building web services and other similar things. Don’t see massive advantages over apache or nginx for plain old web serving, but if you had something more realtime it might be useful. Scales well across machines, perhaps not as much over cores. Other concurrency models exist and are arguably easier to code for.

Problems

  • Young platform, maturity eg have to write your own db driver, or maybe there is a sucky one
  • Debuggability: event loops hard to stack trace unlike threads. Small stack traces are a problem (just like web browser) eg. in exceptions. Could have a debug mode which keeps track of who called me? Memory implications on a live system, though.
  • Single threaded process model. So what about multicore boxen? Start more node processes. Shared nothing model. Relies on OS to manage multiple instances of node. At some point you’ll need multiple machines, so might as well get used to talking a protocol between node instances. But there are other ways of doing concurrency that do scale cleanly over cores and don’t need to serialize and deserialize all the time.
  • Javascript
  • Quite possibly relies on Google’s benevolance in continuing to support v8, though the source is out there.

Useful resources

  • node: a skeptics view. Intro to node with some interesting thoughts on th event model and the particular challenges it brings up.
  • When node goes wrong, detailed insight into how you debug node in the wild.

The last word …

Hehe! There is one of these videos. I love these. Node.js is bad ass rock star tech

Other Javascript technologies

Didn’t look at them in as much depth, but here are quick summaries of some other js things that have ben getting attention.

Angular JS
Rather than sprinkling your html with jquery, angular adds a bit of syntax to the html itself. So you can do nice realtime stuff on the page by just changing your markup. Easier to understand potentially than tracking stuff down in jquery.
Express JS
Express.js is popular web micro framework designed to run on node and clearly heavily influenced by Sinatra. It doesn’t model your database or anything like that. You might make something out of mongo or redis for doing that part.
Backbone JS
Another approach to doing interactivity on pages better than does jquery, backbone.js was originally developed for documentcloud. It isn’t quite an MVC but tries to do similar work.
Jade
Jade is a templating engine for node. The syntax looks like it was inspired by HAML, that is that it is heavily optimised for minimising the amount you have to type.

In finding out more about those projects I found Javascript too, which lists Every js project you should be looking into


Comments

  • Be respectful. You may want to read the comment guidelines before posting.
  • You can use Markdown syntax to format your comments. You can only use level 5 and 6 headings.
  • You can add class="your language" to code blocks to help highlight.js highlight them correctly.

Privacy note: This form will forward your IP address, user agent and referrer to the Akismet, StopForumSpam and Botscout spam filtering services. I don’t log these details. Those services will. I do log everything you type into the form. Full privacy statement.