require('socket.io')(server); io.on('connection', function (socket) { r.connect(config.get('rethinkdb')) .then(function (conn) { r.table('messages') .changes().run(conn) .then(function (cursor) { cursor.each(function (err, row) { socket.emit('message', row.new_val); }); }); }); });