Skip to content

Commit

Permalink
Bump version.
Browse files Browse the repository at this point in the history
  • Loading branch information
stoshiya committed Jun 7, 2014
1 parent da06eb6 commit a92420d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 81 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# socket.io-redis-sample

This is simple node.js application with [Express.js 4.x](https://github.com/visionmedia/express), [Socket.IO 1.x](https://github.com/automattic/socket.io) and [socket.io-redis](https://github.com/Automattic/socket.io-redis).

## Usage

```
$ redis-server &
$ git clone https://github.com/stoshiya/socket.io-redis-sample.git
$ cd socket.io-redis-sample
$ npm install
$ PORT=3000 node app.js &
$ PORT=3001 node app.js &
```

## License

MIT
114 changes: 50 additions & 64 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,61 @@
// Module dependencies.

var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var app = module.exports = express();

app.set('port', process.env.PORT || 3000);

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

/// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

/**
* Module dependencies.
*/

var express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path')
, opts = require('opts')
, config = require('config');

opts.parse([{ short: 'p', long: 'port', description: 'server listen port.', value: true }]);

var app = express();

app.configure(function(){
app.set('port', opts.get('port') || process.env.PORT || 3000);
app.set('views', __dirname + '/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, 'public')));
});

app.configure('development', function () {
app.use(express.errorHandler());
});

app.get('/', routes.index);

var server = http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});

var io = require('socket.io').listen(server);
app.use('/', routes.index);

var RedisStore = require('socket.io/lib/stores/redis')
, redis = require('socket.io/node_modules/redis')
, pub = redis.createClient()
, sub = redis.createClient()
, client = redis.createClient();

pub.on('error', function(err) {
console.log('pub:', err);
});
sub.on('error', function(err) {
console.log('sub:', err);
var server = app.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
client.on('error', function(err) {
console.log('client:', err);
});

if (typeof config.redis !== 'undefined' && typeof config.redis.password !== 'undefined') {
pub.auth(config.redis.password, function(err) { if (err) throw err; });
sub.auth(config.redis.password, function(err) { if (err) throw err; });
client.auth(config.redis.password, function(err) { if (err) throw err; });
}

io.set('store', new RedisStore({
redis: redis,
redisPub: pub,
redisSub: sub,
redisClient: client
}));
var io = require('socket.io')(server);
var redis = require('socket.io-redis');
io.adapter(redis({ host: '127.0.0.1', port: 6379 }));

io.sockets.on('connection', function(socket) {
console.log(io.transports[socket.id].name);

socket.on('message', function(data) {
socket.broadcast.emit('message', data);
});
Expand Down
5 changes: 0 additions & 5 deletions config/default.json.template

This file was deleted.

21 changes: 12 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
{
"name": "socket.io-RedisStore-sample",
"version": "0.0.1",
"name": "socket.io-redis-sample",
"version": "0.0.2",
"private": true,
"scripts": {
"start": "node app"
},
"dependencies": {
"config": "^0.4.36",
"express": "^3.10.3",
"jade": "*",
"opts": "*",
"socket.io": "^0.9.17"
"body-parser": "^1.3.0",
"cookie-parser": "^1.1.0",
"express": "^4.4.1",
"jade": "^1.3.1",
"morgan": "^1.1.1",
"opts": "^1.2.2",
"socket.io": "^1.0.4",
"socket.io-redis": "^0.1.3"
},
"description": "This is simple node.js application with socket.io and RedisStore.",
"description": "This is simple node.js application with Express.js 4, Socket.IO 1.x and socket.io-redis.",
"main": "app.js",
"devDependencies": {},
"repository": {
"type": "git",
"url": "git://github.com/stoshiya/socket.io-RedisStore-sample.git"
"url": "git://github.com/stoshiya/socket.io-redis-sample.git"
},
"author": "Toshiya SAITOH <[email protected]>",
"license": "MIT",
Expand Down
7 changes: 4 additions & 3 deletions public/javascripts/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
(function($) {
var socket = io.connect();
socket.socket.options['connect timeout'] = 2000;
var socket = io();

var start = new Date();

socket.on('connect', function() {
$('p').text('Connection established in ' + (new Date() - start) + 'msec. You are using ' + socket.socket.transport.name + '.');
var index = socket.io.engine.upgrade ? 1 : 0;
$('p').text('Connection established in ' + (new Date() - start) + 'msec. ' +
'You are using ' + socket.io.engine.transports[index] + '.');
$('input').removeAttr('disabled');
$('button').removeAttr('disabled');
});
Expand Down
6 changes: 6 additions & 0 deletions views/error.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extends layout

block content
h1= message
h2= error.status
pre #{error.stack}

0 comments on commit a92420d

Please sign in to comment.