Let's go back to our live-moderation app and add some persistence, first to the questions people ask.

Use the lpush command to add new questions to the list named questions. Do this inside thequestion listener.

var express = require('express');
var app = express.createServer();
var socket = require('socket.io');
var io = socket.listen(app); var redis = require('redis');
var redisClient = redis.createClient(); io.sockets.on('connection', function(client) {
client.on('answer', function(question, answer) {
client.broadcast.emit('answer', question, answer);
}); client.on('question', function(question) {
client.get('question_asked', function(asked) {
if(!asked) {
client.set('question_asked', true);
client.broadcast.emit('question', question); // add the question to the list here
redisClient.lpush('questions', question);
}
});
});
});

Now that we have questions stored in redis, let's emit them whenever a new client connects to the server through socket.io.

Use the lrange command to retrieve an array of questions that represent the questions list in redis. Inside of the lrange callback, use forEach to loop through each question and emit it on the client. Remember, don't use broadcast.emit because we only want to send the questions to the client that is connecting to the server.

var express = require('express');
var app = express.createServer();
var socket = require('socket.io');
var io = socket.listen(app); var redis = require('redis');
var redisClient = redis.createClient(); io.sockets.on('connection', function(client) {
client.on('answer', function(question, answer) {
client.broadcast.emit('answer', question, answer);
}); redisClient.lrange('questions', 0, -1, function(err, messages){
messages.forEach(function(message){
client.emit('question', message);
});
}); client.on('question', function(question) {
client.get('question_asked', function(asked) {
if(!asked) {
client.set('question_asked', true);
client.broadcast.emit('question', question); redisClient.lpush("questions", question);
}
});
});
});

Great work! One last thing though, since every time a new question comes in we store it in thequestions list, we might run into a problem where there are just too many questions stored in that list.

Add a callback to the lpush command, and inside that callback use the ltrim command to make sure the questions list always has at most 20 items.

[Node.js]33. Level 7: Persisting Questions的更多相关文章

  1. [Node.js]30. Level 6: Listen 'Question' from client, and then Answer the Question

    Clients can also answer each other questions, so let's build that feature by first listening for the ...

  2. [Node.js]29. Level 6: Socket.io: Setting up Socket.io server-side & Client socket.io setup

    Below we've already created an express server, but we want to start building a real-time Q&A mod ...

  3. [Node.js]32. Level 7: Working with Lists -- Redis

    As we saw in the video, redis can do more than just simple key-value pairs. We are going to be using ...

  4. [Node.js]31. Level 7: Redis coming for Node.js, Simple Redis Commands

    Let's start practicing using the redis key-value store from our node application. First require the  ...

  5. [Node.js]24. Level 5: Express, Express routes

    Create an express route that responds to GET requests at the URL /tweets that responds with the file ...

  6. [Node.js]23. Level 4: Semantic versioning

    Update the versions on your dependencies to be a little more flexible, adding the ~ in front of your ...

  7. [Node.js]22. Level 4: Dependency

    Add two dependencies to your package.json file, connect and underscore. You'll want to useconnect ve ...

  8. [Node.js]28. Level 5: Express Server

    Now let's create an express server which queries out for this search term and just returns the json. ...

  9. [Node.js]27. Level 5: URL Building & Doing the Request

    Let's create a page which calls the twitter search API and displays the last few results for Code Sc ...

随机推荐

  1. 「NOI2014」购票

    「NOI2014」购票 解题思路 先列出 \(dp\) 式子并稍微转化一下 \[ dp[u] =\min(dp[v]+(dis[u]-dis[v]) \times p[u] + q[u])) \ \ ...

  2. Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分

    D. Friends and Subsequences 题目连接: http://www.codeforces.com/contest/689/problem/D Description Mike a ...

  3. 通过yum来安装vsftpd

    Linux系统:centos6.6.  安装步骤  1.通过yum来安装vsftpd [root@localhost ~]# yum -y install vsftpd 2.设置为开机启动 [root ...

  4. 反射生成SQL语句入门

    今天我们来学习学习通过反射技术来生成SQL语句. 反射提供了封装程序集.模块和类型的对象.您可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型.然后,可以调用类型的方法或访 ...

  5. Node.js学习笔记(3) - 简单的curd

    这个算是不算完结的完结吧,前段时间也是看了好久的Node相关的东西,总想着去整理一下,可是当时也没有时间: 现在看来在整理的话,就有些混乱,自己也懒了,就没在整理,只是简单的记录一下 一.demo的简 ...

  6. Mac使用自带的屏幕共享实现VNC连接KVM时需要输入密码的问题解决

    别试了,下载这个软件VNC-Viewer,苹果自带的那个不行!!! https://www.realvnc.com/en/connect/download/viewer/macos/

  7. Linux下以特定用户运行命令

    方法汇总: 1.su 2.sudo 3.runuser 比较常用的方式:su 示例:su - root -s /bin/sh -c "/usr/local/nginx/sbin/nginx& ...

  8. Hex-Rays Decompiler

    https://www.hex-rays.com/products/decompiler/ We are pleased to present our flagship product, the He ...

  9. java 盒子模型

    http://www.cnblogs.com/xiaohuochai/tag/javascript%E6%80%BB%E7%BB%93/

  10. MVC在基控制器中实现处理Session的逻辑

    当需要跨页面共享信息的时候,Session是首当其冲的选择,最典型的例子就是:在处理登录和购物车逻辑的时候需要用到Session.在MVC中,可以把处理Session的逻辑放在一个泛型基控制器中,但需 ...