Using in Express 4 and express-generator's /bin/www

So here is the deal: I'm trying to use in an express project. After Express Js 4 was lauched, i've updated my express-generator and now the app initial functions goes into ./bin/www file, including those vars (www file contents: )

var server = app.listen(app.get('port'), function() {..}

(check it by npm install -g express-generator and then express myApp

that being said, let's remember how docs ask us to fire it:

var app = require('express').createServer();
var io = require('')(app);

Ok but i can't do it inside app.js, like recommended. This should be done in ./bin/www in order to work. in ./bin/www this is what i can do to get it working:

var io = require('')(server)

Ok this works, but i can't use the io var anywhere else, and i really don't want to put my functions on www file.

I guess this is just basic syntax, but I can't get this to work, not even using module.exports = server or server.exports = server nor = app(io) on www file

So the question is: how can i use having this /bin/www file as starting point of my app?



A little different approach to initiate, it groups all related code in one place:


var socketApi = require('../socketApi');
var io =;


var socket_io = require('');
var io = socket_io();
var socketApi = {}; = io;

io.on('connection', function(socket){
    console.log('A user connected');

socketApi.sendNotification = function() {
    io.sockets.emit('hello', {msg: 'Hello World!'});

module.exports = socketApi;


// Nothing here

In this way all related code in one module and function from it I can invoke from anywhere in application.


It turns out it really was some basic sintax problem.... I got these lines from this chat tutorial...

on ./bin/www, just after var server = app.listen(.....)

var io = require('').listen(server);

so now I create the ../sockets/base.js file and put this little fellow inside it:

module.exports = function (io) { // io stuff here... io.on('conection..... }

Yeah! Now it works... So i guess i really had no option other than starting inside /bin/www , because that is where my http server was started. The goal is that now i can build socket functionality in other file(s), keeping the thing modular, by require('fileHere')(io);



The old "expressjs", everything is happening in the file "app.js". So the binding to server also happens in that file. (BTW, one can still do it the old way, and remove bin/www)

Now with the new expressjs, it needs to happen in the "bin/www" file.

Fortunately, javascript/requirejs made it easy to pass objects around. As Gabriel Hautclocq pointed out, is still "imported" in "app.js" and it gets attached to "app" object via a property = require('')();

The is made live by attaching to it the server in "bin/www"; 

because "app" object is passed into "bin/www" earlier

app = require("../app");

It's really just as simple as


But doing it the "difficult" way ensures that now holds the object.

Now if you need this object also in "routes/index.js" for example, just use the same principle to pass that object around.

First in "app.js", do

app.use('/', require('./routes/index')(;

Then in "routes/index.js"

module.exports = function(io){
    //now you can use io.emit() in this file

    var router = express.Router();

    return router;

So "io" gets injected into "index.js".


Update to Gabriel Hautclocq's response:

In www file, the code should appear as the following due to updates with Attach is now Listen.

 * Create HTTP server.

var server = http.createServer(app);

 * Listen on provided port, on all network interfaces.

server.on('error', onError);
server.on('listening', onListening);

var io =;

Additionally getting that connection to work requires implementing the client side API as well. This isn't Express specific but without it the connect call won't work. The API is included in


Include this file on the front end and test with the following:

var socket = io.connect('http://localhost:3000');

A tutorial for beginners from Cedric Pabst
here are the short basics form the link for an app chat:

using express-generate and the ejs engine usable in every .ejs file standard routing in express-generate

edit the file bin\www and add this; like this

 * Create HTTP server.
var server = http.createServer(app);
 * attach
 * Listen to provided port, on all network interfaces.

edit in app.js

... var app = express();
// call to the app = require('')();

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

//start listen with'connection', function(socket){
console.log('a user connected');

// receive from client (index.ejs) with socket.on
socket.on('new message', function(msg){
      console.log('new message: ' + msg);
      // send to client (index.ejs) with
      // here it reacts direct after receiving a message from the client'chat message' , msg);
module.exports = app;

edit in index.ejs

   <title><%= title %></title>
   <link rel='stylesheet' href='/stylesheets/style.css' />
   <script src="/"></script>
   //include jquery
   <script src="//"></script>
   var socket = io();
   //define functions socket.emit sending to server (app.js) and socket.on receiving 
     // 'new message' is for the id of the socket and $('#new-message') is for the button
     function sendFunction() {
     socket.emit('new message', $('#new-message').val());
    // 'chat message' is for the id of the socket and $('#new-area') is for the text area
   socket.on('chat message', function(msg){

   <h1><%= title %></h1>
   <h3>Welcome to <%= title %></h3>
   <ul id="messages-area"></ul>
   <form id="form" onsubmit="return false;">
     <input id="new-message" type="text" /><button onclick="sendFunction()">Send</button>

Have fun :) and thanks many to Cedric Pabst


Some previous answers are not working and others are overly complicated. Try the following solution instead...

Install server-side and client-side node modules:

npm install --save


Add the following code to bin/www after the server definition, var server = http.createServer(app);:


var io = require('')(server);

io.on("connection", function(socket){
  socket.emit('news', { hello: 'world' });


If using webpack, add the following code to your webpack entry.js file:

var socket = require('')();
socket.on('connect', function(){
  console.log("SOCKET CLIENT CONNECT")

socket.on('news', function(data){
  console.log("SOCKET CLIENT NEWS", data)

Done. Visit your site and check the browser's js developer console.


I have a solution for making available in app.js.


var express      = require( "express"   );
var socket_io    = require( "" );

// Express
var app          = express();

var io           = socket_io();           = io;


// events
io.on( "connection", function( socket )
    console.log( "A user connected" );

module.exports = app;

// Or a shorter version of previous lines:
//    var app = require( "express"   )();
//    var io  = = require( "" )();
//    io.on( "connection", function( socket ) {
//        console.log( "A user connected" );
//    });
//    module.exports = app;



 * Create HTTP server.

var server = http.createServer( app );


var io     =
io.attach( server );


This way, you can access the io variable in your app.js, and even make it available to your routes by defining module.exports as a function which accepts io as a parameter.


module.exports = function(io) {
    var app = require('express');
    var router = app.Router();

    io.on('connection', function(socket) { 

    return router;

Then, pass io into the module after it is setup:


var io = socket_io(); = io;

var routes = require('./routes/index')(io);

After reading through all of the comments, I came up with the following using Server Version: 1.5.0

Issues that I ran into:

  1. var sockIO = require('') should be var sockIO = require('')(). (Credit to: Zhe Hu)

  2. sockIO.attach should be sockIO.listen (Credit to: rickrizzo)


  1. Install with the following command:

    npm install --save
  2. Add the following to app.js:

    var sockIO = require('')();
    app.sockIO = sockIO;
  3. In bin/www, after var server = http.createServer(app), add the following:

    var sockIO = app.sockIO;
  4. To test functionality, in app.js, you can add the line:

    sockIO.on('connection', function(socket){
        console.log('A client connection occurred!');


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.