Socket.io dosn't work

So… for some reason I can’t get socket.io to work with glitch. I am very new to glitch and just tried to recreate a program i have made on my pc.

This is the socket io script in my html file.

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></script>

This is how i require socket io on server.js

var express = require("express");
var app = express(); 
var serv = require("http").Server(app);
var io = require("socket.io")(serv);
app.use(express.static('public'));

app.get('/', function(request, response) {
  response.sendFile(__dirname + '/views/index.html');
});

const listener = app.listen(process.env.PORT, function() {
  console.log('Your app is listening on port ' + listener.address().port);
});

The server runs without any errors but when i try to open a client, socket io can’t connect to the server.

I have no idea what i’m doing wrong. any help is appreciated :slight_smile:

1 Like

Could you please send your project name as it would help us answer your question.

1 Like

oh, yeah sorry
its mega-sejt

1 Like

Hey @nicklasbns I think all that you’re missing here is that you should start the listener with serv rather than app. Both your Express app and Socket.io app can listen using the same server and port, but when you just listen using app your socket server’s not started.

Let us know if that helps. Happy Glitching!

2 Likes

Turns out your trying to make express listen, not the socket.io server.

Instead of

  console.log('Your app is listening on port ' + listener.address().port);
});

use

serv.listen(process.env.PORT);

Hope this helps!

oh no looks like cori beat me to it within 5 seconds!

2 Likes

Thank you guys! it works now :+1:

2 Likes