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. 关于mysql_connect CLIENT_MULTI_RESULTS

    自己写了一个mysql存储过程,以为php有用于调用存储过程的内建函数,查了一下发现只能用mysql_query(call pro())这样的方式,我认为从本质上也就相当于在mysql命令行里执行语句 ...

  2. ubuntu 13.04 编译 安装 升级 gcc 4.9.0 address sanitizer

    @ 前记: 最近查一个线上项目的crash,review代码无果,crash几率低,不可在本地环境重现.之后在线上好几个服务器跑valgrind就不crash了.个人猜测可能是跑valgrind后性能 ...

  3. 如何从Windows远程上传文件到Linux(例如CentOS 7)

    一.先看Linux系统是否安装有vsftp软件(vs是very secure的意思) [root@localhost /]# rpm -qa | grep vsftpdvsftpd-3.0.2-9.e ...

  4. GIT 详解2

    https://segmentfault.com/a/1190000000738398 http://www.cnblogs.com/cposture/p/4903767.html https://g ...

  5. ZOJ3673:1729

    1729 is the natural number following 1728 and preceding 1730. It is also known as the Hardy-Ramanuja ...

  6. ELMAH--Using HTTP Modules and Handlers to Create Pluggable ASP.NET Components 77 out of 90 rated th

    MSDN===http://msdn.microsoft.com/en-us/library/aa479332.aspx PROJECT==https://code.google.com/p/elma ...

  7. MVC使用TempData跨控制器传递信息而无需记住key的名称

    通常情况下,使用TempData需要记住key的名称,本篇体验:通过帮助类,实现对TempData的设置.获取.删除. 关于传递信息的类: namespace MvcApplication1.Mode ...

  8. C#,数据类型扩展 z

    MACD的公式 DIFF : EMA(CLOSE,SHORT) - EMA(CLOSE,LONG);DEA  : EMA(DIFF,M); MACD : 2*(DIFF-DEA), COLORSTIC ...

  9. MiniGUI ial 移植指南

    MiniGUI ial 移植指南 2.1 ial的定义 ial是一个通用的抽象输入接口,可以输入统一的数据的结构,也就是说在MiniGUI的核心代码里输入的数据的格式是固定的,不管输入设备是鼠标 还是 ...

  10. 聊聊并发(四)——深入分析ConcurrentHashMap

    线程不安全的HashMap 因为多线程环境下,使用HashMap进行put操作会引起死循环,导致CPU利用率接近100%,所以在并发情况下不能使用HashMap,如以下代码   final HashM ...