[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.
相关文章
- 前端图片压缩上传(纯js的质量压缩,非长宽压缩)
- 【nodejs原理&源码赏析(7)】【译】Node.js中的事件循环,定时器和process.nextTick
- Node.js模块封装及使用
- Node.js其他模块
- Node.js中Process.nextTick()和setImmediate()的区别
- 1.node.js下载
- 如何查找元素对应事件的js代码
- [Node.js] Level 7. Persisting Data
- [Node.js] Level 6. Socket.io
- [Node.js]27. Level 5: URL Building & Doing the Request
- [Node.js]24. Level 5: Express, Express routes
- 从安装node js到构建一个vue并启动它
- [Node.js] Sequelize Intro - 1
- [Node.js] Level 3 new. Steam
- [Node.js]25. Level 5. Route params
- When.js的原理及快速实现
- Atitit js es5 es6新特性 attilax总结
- paip.最好的脚本语言node js 环境搭建连接mysql
- yenv - 一个使用 JAML 文件管理环境的 Node.js 工具
- log4js-node:node.js日志框架输出到文件
- Node.js学习笔记(二)——Node.js模块化、文件读写、环境变量
- Node.js学习笔记(一)——Node.js概要、NPM与package.json
- 深入Node.js的模块加载机制,手写require函数
- JS中的prototype
- JS事件相关知识点整理
- JS从0到1——813. 打印矩阵