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. BZOJ 1975: [Sdoi2010]魔法猪学院 大水题 第k短路 spfa

    https://www.lydsy.com/JudgeOnline/problem.php?id=1975 我好像到现在了第k短路都不会写,mdzz. 先spfa求出最短路,然后扫点存各种前置路径已经 ...

  2. hdu 2197 推公式

    题意:由0和1组成的串中,不能表示为由几个相同的较小的串连接成的串,称为本原串,有多少个长为n(n<=100000000)的本原串?答案mod2008.例如,100100不是本原串,因为他是由两 ...

  3. 升级Tornado到4后weibo oauth登录不了

    把 Tornado 升级到4后,发现正常运行的微博登录不可以了. 原因是4已经移除 RequestHandler.async_callback and WebSocketHandler.async_c ...

  4. cdoj 841 休生伤杜景死惊开 逆序数/树状数组

    休生伤杜景死惊开 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) 陆伯言军陷八卦 ...

  5. configure: error: lzo enabled but missing

    注意:yum安装的无效,需要手动下载源码安装. wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.06.tar.gz tar zx ...

  6. IIS与Asp.net

    一.IIS 1.绑定 为了将特定的请求映射到相应的网站,IIS允许我们配置“绑定”.所谓“绑定”就是将一个特定的地址.端口号和HTTP主机名对应到特定的网站. IIS7添加绑定的代码如下图所示: 在I ...

  7. 淘宝API得到单个商品的信息,用了淘宝的SDK...

    淘宝api获取到的数据.返回结果可以选择json格式和xml格式的啊.每个api下面都有sdk调用示例哦. 详细:http://wenwen.soso.com/z/q335640192.htm 淘宝开 ...

  8. web office apps 在线预览实践

    摘要 在一些项目中需要在线预览office文档,包括word,excel,ppt等.达到预览文档的目的有很多方法,可以看我之前总结,在线预览的n种方案: [Asp.net]常见word,excel,p ...

  9. .Net高级技术——结构体

    结构体 结构体和类的区别:结构体是值类型,类是引用类型 结构体非常类似于类,但是值类型(拷贝传递),不能被继承 Int32.DateTime等都是结构体,从ValueType继承,值类型. 结构体测试 ...

  10. windows设置共享

    设置共享: 添加用户 点击添加 设置权限 然后别人就可以查看了. 查看共享: 删除共享: