I'm trying to integrate sockets.io into my express app. I used express generator in order to format my application, so I must set up my websocket in bin/www. I found a similar question, and applied the answer found here, instructing me to create my socket object in app.js, attach it to the app object, and access it via the exports in bin/www to attach it to the server.
Here's my app.js file currently:
var express = require('express'),
path = require('path'),
favicon = require('serve-favicon'),
bodyParser = require('body-parser'),
sanitizer = require('sanitizer'),
socket_io = require('socket.io'),
config = require('./lib/config'),
db = require('./lib/db'),
fs = require('fs'),
app = express(),
io = socket_io();
app.io = io;
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(bodyParser.json({strict: false}));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(function(req,res,next){
req.io = io;
next();
});
app.io.sockets.on('connection', function(socket){
console.log('connected!');
});
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({
code: err.status,
err: err.message
}).end();
});
module.exports = app;
This is my bin/www file, regarding the socket.io and http servers:
var app = require('../app');
var debug = require('debug')('fresco:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Socket.io
*/
app.io.attach(server);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
I'm trying to access the server run locally via ws://localhost:3000/
, but I get no response on the client. Am I doing something wrong with my implementation of websockets within express that is breaking my websockets server? I can't imagine why this isn't working, since the object that is created in app.js is being successfully passed to bin/www. Placing a console.log at the end of bin/www shows the following:
{ nsps:
{ '/':
{ name: '/',
server: [Circular],
sockets: [],
connected: {},
fns: [],
ids: 0,
acks: {},
adapter: [Object],
_events: [Object] } },
_path: '/socket.io',
_serveClient: true,
_adapter: [Function: Adapter],
_origins: '*:*',
sockets:
{ name: '/',
server: [Circular],
sockets: [],
connected: {},
fns: [],
ids: 0,
acks: {},
adapter: { nsp: [Circular], rooms: {}, sids: {}, encoder: {} },
_events: { connection: [Function] } },
eio:
{ clients: {},
clientsCount: 0,
pingTimeout: 60000,
pingInterval: 25000,
upgradeTimeout: 10000,
maxHttpBufferSize: 100000000,
transports: [ 'polling', 'websocket' ],
allowUpgrades: true,
allowRequest: [Function],
cookie: 'io',
ws:
{ domain: null,
_events: {},
_maxListeners: undefined,
options: [Object],
path: null,
clients: [] },
_events: { connection: [Function] } },
httpServer:
{ domain: null,
_events:
{ connection: [Function: connectionListener],
clientError: [Function],
close: [Function],
upgrade: [Function],
request: [Function],
error: [Function: onError],
listening: [Function: onListening] },
_maxListeners: undefined,
_connections: 0,
_handle:
{ fd: undefined,
reading: false,
owner: [Circular],
onread: null,
onconnection: [Function: onconnection],
writeQueueSize: 0 },
_usingSlaves: false,
_slaves: [],
allowHalfOpen: true,
pauseOnConnect: false,
httpAllowHalfOpen: false,
timeout: 120000,
_connectionKey: '4:null:3000' },
engine:
{ clients: {},
clientsCount: 0,
pingTimeout: 60000,
pingInterval: 25000,
upgradeTimeout: 10000,
maxHttpBufferSize: 100000000,
transports: [ 'polling', 'websocket' ],
allowUpgrades: true,
allowRequest: [Function],
cookie: 'io',
ws:
{ domain: null,
_events: {},
_maxListeners: undefined,
options: [Object],
path: null,
clients: [] },
_events: { connection: [Function] } } }