[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 ...
随机推荐
- Educational Codeforces Round 44 (Rated for Div. 2)
题目链接:https://codeforces.com/contest/985 ’A.Chess Placing 题意:给了一维的一个棋盘,共有n(n必为偶数)个格子.棋盘上是黑白相间的.现在棋盘上有 ...
- 洛谷.4383.[八省联考2018]林克卡特树lct(树形DP 带权二分)
题目链接 \(Description\) 给定一棵边带权的树.求删掉K条边.再连上K条权为0的边后,新树的最大直径. \(n,K\leq3\times10^5\). \(Solution\) 题目可以 ...
- java 中常用的类
java 中常用的类 Math Math 类,包含用于执行基本数学运算的方法 常用API 取整 l static double abs(double a) 获取double 的绝对值 l sta ...
- 改变手机浏览器(iPhone/Android)上文本输入框的默认弹出键盘
iPhone/iPad和Android提供不同的的键盘输入类型,触发合适的键盘将极大地改善用户体验. 键盘类型 默认: 默认键盘的字母模式 数字: 默认键盘的数字模式,(含小数点等) 邮件: 与默 ...
- 【JavaScript代码实现一】数组去重
function arrayNoDupulate(array) { var hash = {}; var result = []; for(var i=0;i<array.length;i++) ...
- 【对比分析二】Web Storage和cookie的区别
1) 存储空间不同. a) Web Storage能提供5MB的存储空间(不同浏览器的提供的空间不同).Cookie仅4KB. b) Web Storage每个域(包括子域)有独立的存储空间,各 ...
- UVALive 4426 Blast the Enemy! 计算几何求重心
D - Blast the Enemy! Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Subm ...
- php的哈希函数
哈希函数: echo password_hash("rasmuslerdorf", PASSWORD_DEFAULT)."\n"; 验证函数: boolean ...
- .NET面试宝典-基础
经典.net面试题目 建议使用朗读女软件下载,边读边记. 1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有 ...
- SlickSafe.NET 开源权限框架开发指南
前言:本文适用于快速搭建权限系统的用户,尤其适用于希望有良好定义的权限模型建立:系统解决方案是在基于角色访问控制(RBAC)策略基础上的权限访问模型实现,主要完成了后台权限验证逻辑和前端权限数据验证的 ...