45

I'm trying to stream data to the browser. I'm struggling, however, to connect it to the browser. Here's my html:

<ul class="tweets"></ul>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
    var socket = io.connect();
    jQuery(function ($) {
        var tweetList = $('ul.tweets');
        socket.on('tweet', function (data) {
            tweetList .prepend('<li>' + data.user + ': ' + data.text + '</li>'); 
        }); 
    });
</script>

And here's the relevant parts of my app.js:

var express = require('express')
    , twitter = require('ntwitter')
  , http = require('http')
  , path = require('path');

var app = express();

var io = require('socket.io').listen(app);

app.get('/', function (req, res) { res.sendfile(__dirname + '/index.html'); });

app.listen(app.get('port'), function(){
   console.log("Express server listening on port " + app.get('port'));
});

io.sockets.volatile.emit('tweets', {
            user: data.user.screen_name,
            text: data.text,
            geo : geo,
            latitude: latitude,
            longitude: longitude
                });

I installed socket.io 0.9.16 via my packages.json file:

"dependencies": {
    "express": "3.2.6",
    "jade": "*",
    "ntwitter":"0.2.10",
    "socket.io":"0.9.x"
  }

Can anyone help me out here? Why can't it find the file?

Digging a bit deeper. To test the socket, I put this in the app.js:

var socket = io.listen(app);

And I get the error:

TypeError: Object #<Manager> has no method 'listen'
    at Object.<anonymous> (/home/andy/dev/node/mytwittermap/app.js:49:17)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

3 Answers 3

100

Your setup needs to look something like this:

var app     = express();
var server  = require('http').createServer(app);
var io      = require('socket.io').listen(server);
...
server.listen(app.get('port')); // not 'app.listen'!
6
  • 34
    Thanks, your line: "// not 'app.listen'! " Helped a lot !!
    – Jona
    Sep 27, 2014 at 16:51
  • 5
    Thanks for the "not app.listen"
    – Eric
    Oct 24, 2015 at 14:36
  • 4
    2016 here. I had the same error. Details have changed, but the part that we need to do server.listen (not on app) is still valid. Thanks!
    – sra
    Nov 4, 2016 at 8:28
  • @sra Can you have a look at this I just posted? stackoverflow.com/questions/40659396/… Nov 17, 2016 at 16:01
  • (socket.io).listen is not working, had to type (socket.io)(server) directly Jun 13, 2021 at 9:23
25

Can you try this:

var http = require('http');
var app = express();
var server = http.createServer(app);

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

I guess, you will have to instantiate the socket.io server.

1
  • Great - that now seems to be working (lots of good debug info in console). Now onto why it's not displaying in the browser. Thanks very much for your help.
    – babbaggeii
    Jun 7, 2013 at 11:22
11
  • You need to instantiate the socket.io connection and
  • You need to use server.listen() and not app.listen()

Try something like this:

// at the top of app.js
var app     = express();
var server  = require('http').createServer(app);
var io      = require('socket.io').listen(server);

// your code

// at the bottom of app.js
server.listen('3000', () => {
  console.log('Server listening on Port 3000');
})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.