Crhistian
Crhistian

Reputation: 1292

socket.io server not listening to connection event (express server)

I've been banging my head against a couple of guides as well as their docs and it seems like I have everything right. I expect when I navigate to localhost:8080 that the connection event triggers and my console.log message should appear.

// modules =================================================
var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io')(server),
    bodyParser = require('body-parser'),
    methodOverride = require('method-override')
;

// config files ============================================
var db = require('./config/db');

// configuration ===========================================
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); 
app.use(methodOverride('X-HTTP-Method-Override')); 

app.use(express.static(__dirname + '/public'));

// routes  =================================================
require('./app/routes')(app);


io.on('connection', function(){
    console.log("Why won't this display anything");
});

// set port && listen
var port = process.env.PORT || 8080;
server.listen(port);
console.log('Server connected on port ' + port);

exports = module.exports = app;

Upvotes: 0

Views: 170

Answers (1)

jfriend00
jfriend00

Reputation: 708106

To see a socket.io connection on the server, you need client-side code in your web page that creates the socket.io connection.

That would typically look like this:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io();
  // do more things with the connection here
</script>

Upvotes: 1

Related Questions