[Node.js]33. Level 7: Persisting Questions
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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [Node.js]22. Level 4: Dependency
Add two dependencies to your package.json file, connect and underscore. You'll want to useconnect ve ...
- [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. ...
- [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 ...
随机推荐
- nginx 站点代理,负载均衡
nginx服务器IP是192.168.1.201 web服务器 IP 192.168.1.200,192.168.1.199 1.主配置文件是/etc/nginx/下的nginx.conf,另外一个是 ...
- ASP.NET MVC HttpVerbs.Delete/Put Routes not firing
原文地址: https://weblog.west-wind.com/posts/2015/Apr/09/ASPNET-MVC-HttpVerbsDeletePut-Routes-not-firing ...
- js的栈与堆
JavaScript中基本数据类型和引用数据类型的区别 这是我引用别人的 觉得很好 1.基本数据类型和引用数据类型 ECMAScript包括两个不同类型的值:基本数据类型和引用数据类型. 基本 ...
- jQuery和$、jQuery(function(){})和(function(){})(jQuery)
1.$是JQuery的别名 ,在使用上是一样的,两者可以互换位置. $==jQuery; //true $===jQuery; //true 2.jQuery(function(){ ....... ...
- wamp经典安装
1,根据综述 本机 注意,现在apache2.2不能和5.5php, 2.4apache和5.5php就可以 window10 64位 vc14 apache 2.4.23 注意,虚拟 ...
- js判断手机端和pc端
var browser = { versions: function() { var u = navigator.userAgent, app = navigator.appVersion; retu ...
- 使用Bootstrap 3开发响应式网站实践01,前期准备、导航区域等
"使用Bootstrap 3开发响应式网站实践"系列,将使用Bootstrap 3.2制作一个自适应网站,无论是在电脑.平板,还是手机上,都呈现比较好的效果.在电脑浏览器上的最终效 ...
- 【docker-compose】docker-compose.yml文本内容详解 + docker-compose命令详解 + docker-compose启动服务容器时区设置
参考地址:https://blog.csdn.net/Kiloveyousmile/article/details/79830810 参考地址:https://docs.docker.com/comp ...
- Windows Embedded Compact 7初体验
Windows Embedded Compact 7初体验 Windows Embedded Compact 7已经出来半年多了,一直没时间搞.最近它又出了Refresh的版本,电脑也换了个1T的硬盘 ...
- Mongodb后台daemon方式启动
Mongodb可以通过命令行方式和配置文件的方式来启动,具体命令如下: 命令行: [root@localhost mongodb]# ./bin/mongod --dbpath=/data/db 配置 ...