Samu Németh
Samu Németh

Reputation: 556

Socket.io files missing?

It's probably obvious, but I'm on this for more than 3 hours now, but I can't figure out the problem. I just want to make a simple webpage, that can communicate with the server-side code. I have the latest version of all the libraries.

My server-side code is:

//import libaries
global.express = require('express');
global.http = require('http');
global.socketio = require('socket.io');

global.app = express();
global.server = http.createServer(app);
global.io = socketio(server);

//serve files
app.use(express.static('public'));

//start listening
app.listen(80, () => {
    console.log('listening...');
});

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

And my client-side code looks like this:

<script src="/socket.io/socket.io.js"></script>
<script>
    let socket = io();
</script>
<script src="sketch.js"></script>

This should work, but I get this error every time (in the client side-console):

GET http://localhost/socket.io/socket.io.js net::ERR_ABORTED 404 (Not Found)

I know there is a lot of questions like this, but they are usually outdated.

PS: If I remove the socket part, express functions properly.

Upvotes: 0

Views: 116

Answers (1)

i_arber
i_arber

Reputation: 178

Okay so it seems like you're creating your own http server and then attaching socket.io to it. But then instead of listening to said server, you listen to Express. Either do

server.listen(80, () => {
    console.log('listening...');
});

or just use Express' server by

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

Also, if you're building a simple website, you might not need socket.io at all.

Upvotes: 1

Related Questions