Jul 14

Node.js notes: Code School’s levels 6-7

This is a collection of my notes on Code School’s, Realtime Web with Node.js course. It follows the first set of notes on setting up a node server, using curl, read streams and write streams (levels 1-3), and the second set on using NPM, putting your dependencies in package.json and setting up the Express framework (levels 4-5).

Level 6

Socket.io allows you to create duplexed websocket connections in your node.js application. This allows data to be sent from client to server, from server to client or both simultaneously.


After installing socket.io with NPM, you need to require it on the server side.

Then you need to include the library and do similar setup on the client side…

Sending Messages

You can send them either way. Here’s an example from server to client:

And to receive it:

Broadcasting to every client

One neat thing that socket.io can do that a traditional websocket can’t do so easily is broadcast a message to every client.

Saving and retrieving data on the socket with get and set

Level 7

Redis is a key value store. It’s much faster than a traditional relational database on big data sets but it doesn’t store deep data structures, just keys and values. It offers strings, lists, sets, sorted sets and hashes, but no nested structures. Most importantly, Redis is non-blocking, so you your node app using redis won’t be halted while retrieving data.

The Redis website has a fantastic interactive reference where you can try out the commands as if you were in a REPL. It’s so good that there’s little need to take notes on any of the commands.

Setting up Redis

I would have added to Code School’s instructions a bit lacking here. You need a few installs to start using Redis comfortably–

  • redis – the library itself
  • redis-server – the server that you have to start for your node program to be able to access the db
  • redis-cli – the command line interface so you can inspect the database and experiment while figuring out how to make your node app do what you want it to do

Get them all:

Basic usage examples

Since there are only a few data types and the querying options aren’t so complex, learning Redis is pretty simple compared to something like MySQL or mongoDB.

This entry was tagged , , , , .
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *