I'm making a node.js application that uses socket.io and express.
The code looks like this-
const express=require('express');
const app=express();
const http=require('http').Server(app);
app.use(express.static('public'));
const io=require('socket.io')(http,{
cors:{origin:'*'}
});
http.listen(3000,()=>{
console.log('listening to port 3000');
});
//when connection made
io.on('connection',(socket)=>{
console.log('connection made!');
});
Here,public is a folder container all the html files for the website.
Now,when I run it locally on my pc,it works fine But when I deployed it on the glitch.com servers,i get this error:
Error: listen EADDREINUSE: address already in use :::3000
As far as i know,3000 is the only available port in glitch and I cannot use another port.
Also,the questions on stack overflow related to this topic doesn't help me since most of the answers are related to killing the running tasks themselves and I don't have permission to do so on the server.
This often happens when your server is still running in error, try switching listening to 3001 and see if there is still the error
free your 3000 port:
Linux: fuser -k 3000/tcp
According to this question on glitch.com's forums you might have two services listening to the same port, but that doesn't seem to be the case.
You should also be able to change the default listening port by setting the PORT environment variable in your .env file if you have one. Trying a different port should solve the problem
EDIT: Apparently the problem could be the initial configuration of the server, you can try setting everything up like this:
const app = require('express')();
const server = require('http').createServer(app); <--
const io = require('socket.io')(server);
io.on('connection', () => { /* your code */ });
server.listen(process.env.PORT, () => {/* your code */});
(Got this from socket.io github page)
If this doesn't work either you should try asking on glitch.com's support forums, one of their technicians will help you better troubleshoot your problem, they have all the necessary tools