Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser can't get socket.io.js file using express

I have problem with socket.io examples. My browser can't get socket.io.js file (404 error in console).


Code that work:

server.js

var app = require('express').createServer()
  , io = require('socket.io').listen(81);

app.listen(80);

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

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

index.html

<script src="http://192.168.1.104:81/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://192.168.1.104:81');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>


But this one not:

server.js

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

app.listen(80);

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

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

index.html

<script src="http://192.168.1.104:80/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://192.168.1.104:80');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

In this case my browser can't get socket.io.js file.

like image 687
Jakub Martin Avatar asked Apr 17 '12 07:04

Jakub Martin


People also ask

Can you use Socket.IO with Express?

Socket.IO can be used based on the Express server just as easily as it can run on a standard Node HTTP server. In this section, we will fire the Express server and ensure that it can talk to the client side via Socket.IO.

How do I connect Socket.IO to node js?

In order to do it, you need to create an index. js file and install socket.io and express. You can use the following command: touch index. js && npm install express socket.io && npm install --save-dev nodemon .

What is socket Io in Node JS?

Socket.IO enables real-time, bidirectional and event-based communication. It works on every platform, browser or device, focusing equally on reliability and speed. Socket.IO is a JavaScript library for real-time web applications.

What is the difference between WebSocket and WebSocket Io?

WebSocket provides full-duplex communication on TCP connections, Socket.io provides the event-based communication between browser and server. WebSocket does not support Proxy and load balancer, with Socket.io a connection can be established in the presence of proxies and load balancers.

How to test a socket application with nodemon?

You can test it by running the nodemon command, this command will run the server on localhost:8080. If you go to your browser and enter localhost:8080, you can see that the application is running. Now, let’s continue with socket.io. The critical parts of this code block are the io.on and socket.emit functions.


2 Answers

EDIT : all the below text is wrong until the next "EDIT". Leaving it there as a trace...

there is one thing you should know, two things you should do + all needed Express doc is here :

  1. Express filter/handle every access to your node server. It means that when you're trying to access your socket.io script file, Express tries to find it in the routes you declared and fails as you didn't (and you were right not to).
  2. Most important : declare a static, non "computed" folder in express where you will put all your static files (css, client scripts, images) :

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

    This line must be put in you app.configure call, before app.use(app.router) (I actually put it first)

    I like to have this /static/scripts ; /static/css ; /static/img folder organisation but you're free to adapt to your needs.

  3. Change the link to the socket.io script file to a relative path (optional but strongly advised) : src='/static/scripts/socket.io/socket.io.js'

EDIT : I am wrong, very very wrong and I am sorry for that. Socket.io generates the different path / files needed and you don't have to declare them nor to copy any client script files.

Please try switching the <script src="http://192.168.1.104:81/socket.io/socket.io.js"></script> client line to the normal relative one <script src="/socket.io/socket.io.js"></script> because that's the only difference between your code and the express guide code.

like image 178
Arnaud Rinquin Avatar answered Oct 29 '22 11:10

Arnaud Rinquin


What Express version are you using?

The API has changed from Express 2.x to 3.x, so the answer is in the Socket.IO compatibility section at the Migrating from 2.x to 3.x wiki:

Socket.IO's .listen() method takes an http.Server instance as an argument.
As of 3.x, the return value of express() is not an http.Server instance. To get Socket.IO working with Express 3.x, make sure you manually create and pass your http.Server instance to Socket.IO's .listen() method:

var app = express()
  , http = require('http')
  , server = http.createServer(app)
  , io = require('socket.io').listen(server);

server.listen(3000);
like image 43
Marius Butuc Avatar answered Oct 29 '22 12:10

Marius Butuc