javascript - Using Node.js to retrieve data from Redis through an AJAX request -
i'm going through node
, express
, & socket.io
chat tutorial. decided use redis
store chat history , have set information correctly posting database. trying access information use on client-side (in case i'm trying access list of users in chat can show them side of chat). using $.getjson
make get
request. right have setup file tries access has json
object : {"dog" : "2","cat":"3"}
test it, , working, i'm not sure go there because anytime try adding function file, if specify return json object , call function, request stops returning correct information.
for example tried :
var data = function(){ return {"dog" : "2","cat":"3"} } data();
and doesn't return ( understand when make get
request function isn't run, doesn't return text, , if doesn't run function i'm not sure how can access redis file)
here's i'm thinking:
var redis = require('redis') //figure out how access redis client have @ localhost:6379, var db = redis.x //and call (for example) db.smembers('onlineusers') , returned object can iterate through
here's relevant code:
server.js:
var jade = require('jade'); var port = 8080; var redis = require('redis'); var db = redis.createclient(); var pub = redis.createclient(); var sub = redis.createclient(); var http = require('http'); var express = require('express'); var app = express(); var server = http.createserver(app); var io = require('socket.io').listen(server); server.listen(port, function(){ console.log("now connected on localhost:" + port) }); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.set("view options", {layout: false}); app.use(express.static(__dirname + '/public')); app.get('/', function(req, res){ res.render('home'); }); io.sockets.on('connection', function(client){ sub.subscribe("chatting"); sub.on("message", function (channel, message) { console.log("message received on server publish"); client.send(message); }); client.on("sendmessage", function(msg) { pub.publish("chatting",msg); }); client.on("setusername", function(user){ pub.publish("chatting","a new user in connected:" + user); db.sadd("onlineusers",user); } ); client.on('disconnect', function () { sub.quit(); pub.publish("chatting","user disconnected :" + client.id); }); });
script.js:
$(document).ready( function(){ $client = io.connect(); initialize(); }); var setusername = function(){ var username = $("#usernameinput").val(); if (username) { var user = username; $client.emit('setusername', username); $('#chatcontrols').show(); $('#usernameinput').hide(); $('#usernameset').hide(); showcurrentusers(); } } var showcurrentusers = function(){ $('#list_of_users').empty(); $.getjson('getusers.js', function(data){ (var = 0; < data.length; i++){ $('list_of_users').append("<li>"+data[i]+"</li>") } }) } var sendmessage = function(){ var msg = $('#messageinput').val(); var username = $("#usernameinput").val(); if (msg) { var data = {msg: msg, user: username} $client.emit('message', data); addmessage(data); $('#messageinput').val(''); // populate(username,msg); } } var addmessage = function(data) { $("#chatentries").append('<div class="message"><p>' + data.user + ' : ' + data.msg + '</p></div>'); } // var populate = function(username,msg) { // var data ; // } var initialize = function(){ $("#chatcontrols").hide(); $("#usernameset").on('click', setusername); $("#submit").on('click',sendmessage); showcurrentusers(); }
and right getusers.js
file has in is:
{"dog" : "2","cat":"3"}
it looks you're expecting call $.getjson load , execute javascript loads. doesn't work way. need make node endpoint (via route) renders json. node endpoint data manipulation / querying redis:
node:
in routes.js:
app.get('/chatdata', chatcontroller.getchatdata);
in chatcontroller.js (manipulate, create data here)
exports.getchatdata = function (req, res) { var data = function(){ return {"dog" : "2","cat":"3"} }; res.json(data); };
front-end
$.getjson('getchatdata', function(data){ //... })
Comments
Post a Comment