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.

Setup

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

// in your app.js
var socket = require('socket.io');
var app = express.createServer();
var io = socket.listen.app();

io.sockets.on('connection' function(client) {
  console.log('Client connection established...');
});

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

// inside your index.html (presumably loaded somewhere with express)
<script src="/socket.io/socket.io.js"></script>
<script>
  var server = io.connect('http://localhost:8080');
</script>

Sending Messages

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

// app.js
io.sockets.on('connection', function(client) {
  client.emit('messages', {greeting: 'Hello, client!'});
});

And to receive it:

//index.html
var server = io.connect(URL);
server.on('messages', function(data) {
  alert(data.greeting);  // will pop up saying, "Hello, client!"
});

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.

client.broadcast.emit('messages', data);

Saving and retrieving data on the socket with get and set

client.set('location', 'Rome');
client.get('news', function(err, updates) {
  //do stuff with updates
});

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:

npm install -g redis
npm install -g redis-server
npm install -g redis-cli

Basic usage examples

var redis = require('redis');
var redisClient = redis.createClient();

redisClient.set('PI', 3.14159265);
redisClient.get('PI');   // -&gt; 3.14159265

redisClient.lpush('World Wonder', 'Great Pyramid of Giza');
redisClient.lpush('World Wonder', 'Hanging Gardens of Babylon');
redisClient.lpush('World Wonder', 'Statue of Zeus at Olympia');
redisClient.lpush('World Wonder', 'Temple of Artemis at Ephesus');
redisClient.lpush('World Wonder', 'Mausoleum of Maussollos at Halicarnassus');
redisClient.lpush('World Wonder', 'Colossus of Rhodes');
redisClient.lpush('World Wonder', 'Lighthouse of Alexandria');

redisClient.lindex('World Wonder', 6);  // -> 'Colossus of Rhodes'
redisClient.lrange('World Wonder', 2,4);  // 2nd, 3rd and 4th elements, ->
//['Hanging Gardens of Babylon', 'Statue of Zeus at Olympia',  'Temple of Artemis at Ephesus']

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.

Leave a Reply

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

1 reply