├── .gitignore ├── LICENSE ├── README.md ├── app.js ├── package.json ├── public ├── javascripts │ └── index.js └── stylesheets │ └── style.css ├── routes └── index.js └── views ├── error.jade ├── index.jade └── layout.jade /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | .idea 14 | 15 | npm-debug.log 16 | node_modules 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Toshiya SAITOH 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # socket.io-redis-sample 2 | 3 | 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). 4 | 5 | ## Usage 6 | 7 | ``` 8 | $ redis-server & 9 | $ git clone https://github.com/stoshiya/socket.io-redis-sample.git 10 | $ cd socket.io-redis-sample 11 | $ npm install 12 | $ PORT=3000 node app.js & 13 | $ PORT=3001 node app.js & 14 | ``` 15 | 16 | ## License 17 | 18 | MIT -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | // Module dependencies. 2 | 3 | var express = require('express'); 4 | var path = require('path'); 5 | var logger = require('morgan'); 6 | var cookieParser = require('cookie-parser'); 7 | var bodyParser = require('body-parser'); 8 | 9 | var routes = require('./routes/index'); 10 | var app = module.exports = express(); 11 | 12 | app.set('port', process.env.PORT || 3000); 13 | 14 | // view engine setup 15 | app.set('views', path.join(__dirname, 'views')); 16 | app.set('view engine', 'jade'); 17 | 18 | app.use(logger('dev')); 19 | app.use(bodyParser.json()); 20 | app.use(bodyParser.urlencoded({ extended: false })); 21 | app.use(cookieParser()); 22 | app.use(express.static(path.join(__dirname, 'public'))); 23 | 24 | /// error handlers 25 | 26 | // development error handler 27 | // will print stacktrace 28 | if (app.get('env') === 'development') { 29 | app.use(function(err, req, res, next) { 30 | res.status(err.status || 500); 31 | res.render('error', { 32 | message: err.message, 33 | error: err 34 | }); 35 | }); 36 | } 37 | 38 | // production error handler 39 | // no stacktraces leaked to user 40 | app.use(function(err, req, res, next) { 41 | res.status(err.status || 500); 42 | res.render('error', { 43 | message: err.message, 44 | error: {} 45 | }); 46 | }); 47 | 48 | app.use('/', routes.index); 49 | 50 | var server = app.listen(app.get('port'), function(){ 51 | console.log('Express server listening on port ' + app.get('port')); 52 | }); 53 | 54 | var io = require('socket.io')(server); 55 | var redis = require('socket.io-redis'); 56 | io.adapter(redis({ host: '127.0.0.1', port: 6379 })); 57 | 58 | io.sockets.on('connection', function(socket) { 59 | socket.on('message', function(data) { 60 | socket.broadcast.emit('message', data); 61 | }); 62 | }); 63 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "socket.io-redis-sample", 3 | "version": "0.0.2", 4 | "private": true, 5 | "scripts": { 6 | "start": "node app" 7 | }, 8 | "dependencies": { 9 | "body-parser": "^1.7.0", 10 | "cookie-parser": "^1.3.2", 11 | "express": "^4.8.7", 12 | "jade": "^1.6.0", 13 | "morgan": "^1.3.0", 14 | "opts": "^1.2.2", 15 | "socket.io": "^1.0.6", 16 | "socket.io-redis": "^0.1.3" 17 | }, 18 | "description": "This is simple node.js application with Express.js 4, Socket.IO 1.x and socket.io-redis.", 19 | "main": "app.js", 20 | "devDependencies": {}, 21 | "repository": { 22 | "type": "git", 23 | "url": "git://github.com/stoshiya/socket.io-redis-sample.git" 24 | }, 25 | "author": "Toshiya SAITOH ", 26 | "license": "MIT", 27 | "bugs": { 28 | "url": "https://github.com/stoshiya/socket.io-RedisStore-sample/issues" 29 | }, 30 | "homepage": "https://github.com/stoshiya/socket.io-RedisStore-sample" 31 | } 32 | -------------------------------------------------------------------------------- /public/javascripts/index.js: -------------------------------------------------------------------------------- 1 | (function($) { 2 | var socket = io(); 3 | 4 | var start = new Date(); 5 | 6 | socket.on('connect', function() { 7 | var index = socket.io.engine.upgrade ? 1 : 0; 8 | $('p').text('Connection established in ' + (new Date() - start) + 'msec. ' + 9 | 'You are using ' + socket.io.engine.transports[index] + '.'); 10 | $('input').removeAttr('disabled'); 11 | $('button').removeAttr('disabled'); 12 | }); 13 | 14 | socket.on('message', function(data) { 15 | $('div.message > ul').append('
  • ' + new Date().toString() + ': ' + data + '
  • '); 16 | }); 17 | 18 | $('input').keydown(function(e) { 19 | if (e.keyCode === 13) { // press ENTER. 20 | submitHandler(); 21 | } 22 | }); 23 | 24 | $('button').click(function() { 25 | submitHandler(); 26 | }); 27 | 28 | function submitHandler() { 29 | var text = $('input').val(); 30 | if (text.length > 0) { 31 | socket.emit('message', text); 32 | $('input').val(''); 33 | $('div.message > ul').append('
  • ' + new Date().toString() + ': ' + text + '
  • '); 34 | } 35 | } 36 | }(jQuery)); 37 | -------------------------------------------------------------------------------- /public/stylesheets/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | padding: 50px; 3 | font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; 4 | } 5 | 6 | a { 7 | color: #00B7FF; 8 | } 9 | -------------------------------------------------------------------------------- /routes/index.js: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * GET home page. 4 | */ 5 | 6 | exports.index = function(req, res){ 7 | res.render('index', { title: 'Express' }); 8 | }; 9 | -------------------------------------------------------------------------------- /views/error.jade: -------------------------------------------------------------------------------- 1 | extends layout 2 | 3 | block content 4 | h1= message 5 | h2= error.status 6 | pre #{error.stack} 7 | -------------------------------------------------------------------------------- /views/index.jade: -------------------------------------------------------------------------------- 1 | extends layout 2 | 3 | block content 4 | h1= title 5 | p Connecting... 6 | 7 | input(type='text', disabled) 8 | button(disabled) submit 9 | div.message 10 | ul 11 | 12 | script(src='http://code.jquery.com/jquery-1.10.1.min.js') 13 | script(src='/socket.io/socket.io.js') 14 | script(src='/javascripts/index.js') 15 | -------------------------------------------------------------------------------- /views/layout.jade: -------------------------------------------------------------------------------- 1 | doctype html 2 | html 3 | head 4 | title= title 5 | link(rel='stylesheet', href='/stylesheets/style.css') 6 | body 7 | block content 8 | --------------------------------------------------------------------------------