angularjs - Retrieve data from Redis and display them on the frontend -
i push , retrieve data redis list in nodejs(express), cannot send them , display on frontend (angular).
i push message lpush method , retrieve lrange , want send these data frontend (with express -> app.get{}). on frontend use angular angular-socket-io factory.
please take @ server.js file bellow.
var express = require('express'); var app = express(); app.use('/', express.static('../app/')); app.use('/bower_components', express.static('../bower_components/')); var server = require('http').createserver(app); var io = require('socket.io').listen(server); var bodyparser = require('body-parser'); var jsonparser = bodyparser.json(); var urlencodedparser = bodyparser.urlencoded({ extended: false }); var redis = require('redis') var redisclient = redis.createclient(); /** * store message in redis * * @function storemessage * @param {string} name * @param {object} data */ function storemessage (name, data) { var message = json.stringify({name:name, data:data}); // store 10 messages redisclient.lpush('messages', message, function(error) { if (error) throw error; redisclient.ltrim('messages', 0, 10); }); }; io.sockets.on('connection', function (client) { /** * when users join, set nickname , broadcast here * add user redis */ redisclient.lrange('messages', 0, -1, function( error, messages) { messages = messages.reverse(); messages.foreach(function(message) { message = json.parse(message); client.emit('messages',message.name + ' : ' + message.data); }); }); }); //!!trouble here?? -> send data frontend app.get('/messages', function (err, res) { 'use strict'; messages.foreach(function(res) { client.emit("messages", res.name + ": " + res.data); }); res.send(messages); }); server.listen(3000, function () { 'use strict'; });
Comments
Post a Comment