Skip to content

Instantly share code, notes, and snippets.

@coderarity
Created November 15, 2012 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coderarity/59fa62eff63ccd9a135a to your computer and use it in GitHub Desktop.
Save coderarity/59fa62eff63ccd9a135a to your computer and use it in GitHub Desktop.
Redis Connection Problem
#!/usr/bin/env node
/**
* Module dependencies.
*/
var express = require('express'),
path = require('path'),
routes = require(path.join('..','routes')),
http = require('http'),
path = require('path');
var redisConnection = (function() {
var redis = require("redis").createClient("6379", "dev.ninjasquare.com");
return redis;
})();
var RedisStore = require('socket.io/lib/stores/redis');
store = new RedisStore({
redisPub : redisConnection()
, redisSub : redisConnection()
, redisClient : redisConnection()
});
var app = express();
app.configure(function(){
app.set('port', 8080);
app.set('views', path.join(__dirname , path.join('..','views')));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, path.join('..','public'))));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
//TODO: when express 3 updates check if there is a better way to attach socket.io
var server = http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port %d in %s mode", app.get('port'), app.settings.env);
});
io = require('socket.io').listen(server);
io.set('store', store);
//Socket.io emits this event when a connection is made.
io.sockets.on('connection', function (socket) {
// Emit a message to send it to the client.
socket.emit('ping', { msg: 'Hello. I know socket.io.' });
// Print messages from the client.
socket.on('pong', function (data) {
console.log(data.msg);
});
socket.on('hello', function (data) {
socket.broadcast.emit('hello', data);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment