19

I'm actually working on a little project, where i'm supposed to recreate a drawing multiplayer game with node.js, mongoDB, socket.io and canvas.

The drawer is working like a charm, and the server seems to work well too. I got my register/login/sessions and database up and working, the only problem is socket.io. When an user is joining the game room, he can see the drawer and tools, but no connection. Why ? The browser can't find socket.io.js.

What I did :

I verified if it was installed, it is with npm install socket.io. I checked if the server was starting it when turning the server on : Got "socket.io started" in my console. I checked my HTML code, here it is :

<script type="text/javascript" src="/socket.io/socket.io.js"></script>

According to the billions of tutorials/dev sites/help subjects, this is supposed to work. But it's not. When opening the console of my browser, I got this :

X GET http://localhost:1337/socket.io/socket.io.js NOT FOUND.

I don't know where is the problem, I can't figure this out and it's giving me a huge headache.. So I'm here.

Thanks in advance for helping ! :)

YumeYume
  • 981
  • 2
  • 12
  • 33
  • Do you have nodejs serving static files properly? Where are you serving socket.io out of? – NG. Jan 26 '14 at 15:12
  • At the root of my project is the server.js file. In the variables, I got var io = require('socket.io').listen(server); Then, when the user reaches the page where socket.io is required, the server do this : io.sockets.on('connection', function(socket) {console.log('Client connected'); }) And for the static files, I think it's good too, I got this : .use('/public', express.static(__dirname + '/public')) – YumeYume Jan 26 '14 at 15:23
  • That file is not served with the static middleware, but by the `socket.io` module itself. It's probably a misconfiguration, can you show the code where you initialize Express and `socket.io`? – robertklep Jan 26 '14 at 16:39
  • express = require('express'), server = express(), io = require('socket.io').listen(server), [other stuff for DB models etc] I'm pretty new to this, and not very good for web development, I probably (obviously ?) misconfigured something :) – YumeYume Jan 26 '14 at 16:56
  • possible duplicate of [socket.io.js not found](http://stackoverflow.com/questions/10191048/socket-io-js-not-found) – mpromonet Nov 08 '14 at 15:36

2 Answers2

66

Given the code in your comment, you're not using the correct variable for initializing socket.io.

Try this:

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

So instead of having socket.io 'listen' on the Express app instance, it should listen to what app.listen(...) returns (which happens to be an http.Server instance).

robertklep
  • 198,204
  • 35
  • 394
  • 381
  • Yes, this is working ! I forgot about the app/server thing. Now my socket.io.js is detected ! It's almost working. Almost, because in my server.js, when an user gets into the page using socket.io, I do this : io.sockets.on('connection', function(socket) { console.log('User connected'); But instead of showing me this, I got this : debug - served static content /socket.io.js. Any idea about this ? If not, thanks a lot, you really helped me ! – YumeYume Jan 26 '14 at 17:14
  • @YumeYume it depends on where exactly you put that code. It should (usually) be at the top level of your main JS file; or differently put, *not* in an Express route handler or anything like that. – robertklep Jan 26 '14 at 17:19
  • Okay, i'll try it, but the main problem of this subject is resolved. Thanks for helping me ! – YumeYume Jan 26 '14 at 18:27
  • This solved my problem i used `var io = require('socket.io').listen();` instead of `var io = require('socket.io').listen(server);`, causing a **Cannot GET /socket.io/socket.io.js** error. I forgot to pass the `server` object to `socket.io`. – leonard.javiniar Jan 31 '15 at 04:41
2

For anyone landing here because they're going through the v4.x socket.io get started example, all you need to do is add another endpoint to your index.js file

index.js

const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html');
});

// add this
app.get('/socket.io/socket.io.js', (req, res) => {
  res.sendFile(__dirname + '/node_modules/socket.io/client-dist/socket.io.js');
});
///

io.on('connection', (socket) => {
  console.log('a user connected');
});

server.listen(3000, () => {
  console.log('listening on *:3000');
});
lasec0203
  • 2,422
  • 1
  • 21
  • 36