Permalink
Browse files

upgrade redis client and socket.io

  • Loading branch information...
1 parent ba7bdd3 commit 6abd1d669539a23b343fb6b63ef6a704f4bcf0a8 @mnutt committed Feb 17, 2012
Showing with 19 additions and 26 deletions.
  1. +14 −20 app.js
  2. +2 −2 package.json
  3. +3 −4 public/index.html
View
34 app.js
@@ -1,34 +1,28 @@
-require.paths.unshift(__dirname + "/node_modules");
+var redis = require("redis"),
+ http = require("http"),
+ socketio = require("socket.io"),
+ client = redis.createClient();
+
+var io = socketio.listen(8080, "0.0.0.0")
+// Redis channel to listen on
var channel = process.argv[2];
if(!channel) {
console.log("Usage: node app.js [redis channel]");
process.exit(1);
}
-var redis = require("redis"),
- http = require("http"),
- io = require("socket.io"),
- client = redis.createClient();
-
-// Set up websockets
-var server = http.createServer(function(req, res) {
- res.writeHeader(200, {'Content-Type': 'text/html'});
- res.writeBody('<h1>Hello world</h1>');
- res.finish();
-});
-server.listen(8080, "0.0.0.0")
-var socket = io.listen(server);
-socket.on("connection", function(client) {
+// For debugging only
+io.sockets.on("connection", function(socket) {
console.log("New client!");
});
-// Watch redis
-console.log("Watching channel: " + channel);
-
client.on("message", function(channel, message) {
- console.log(message.toString());
- socket.broadcast(message.toString());
+ // You could emit this on a uid rather than 'message' to send
+ // different messages to different users
+ io.sockets.emit('message', message.toString());
});
+// Watch redis
client.subscribe(channel);
+console.log("Watching channel: " + channel);
View
@@ -3,8 +3,8 @@
"description" : "Watch a redis pubsub channel and broadcast it to websocket clients",
"version" : "0.0.1",
"dependencies" : {
- "redis" : "0.5.9",
- "socket.io" : "0.6.16"
+ "redis" : "0.7.1",
+ "socket.io" : "0.8.7"
},
"repository" : {
"type" : "git",
View
@@ -6,15 +6,14 @@
<script>
$(document).ready(function() {
- socket = new io.Socket('localhost', {port: 8080});
- socket.on('message', function(data){
+ socket = io.connect("http://localhost:8080");
+ socket.on('message', function(data) {
+ console.log(data);
var div = $("<div></div>");
div.text(data);
$("#messages").prepend(div);
});
-
- socket.connect();
});
</script>
</head>

0 comments on commit 6abd1d6

Please sign in to comment.