Reputation: 1769
So I have a finished nodejs + socket.io + reactjs project that is working on my machine. I can open different tabs or browsers in my machine and test it out, and its working. However I wanted to test it on other machine within my local area network. When I connect it through the wifi on other machine to my network ip address, I can see the page. However, the socket.io is not working. When I open the console in chrome (other machine), this is the error I see:
GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=LMXV0Uf net::ERR_CONNECTION_REFUSED
This is my node app-server.js:
app.use(express.static('./public'));
app.use(express.static('./node_modules/bootstrap/dist'));
var server = app.listen(3000);
var io = require('socket.io').listen(server);
How can I solve this issue? Also what is the recommended way to deploy in production so that socket.io will work.
Upvotes: 1
Views: 2686
Reputation: 20624
There are two parts to the socket app.. the stuff that lives on some single server somewhere, and the client side code that gets distributed to other computers, who need to connect back to the server to make the socket connection.
Your server code probably does not need to change. If you're able to reach it via some IP address that means it's serving correctly.
Let's say you can access the website via http://1.2.3.4:3000
Somewhere in your client side code you'll have something like:
import io from 'socket.io-client'
let serverUrl = 'localhost:3000'
let socket = io(serverUrl)
You'll need to change serverUrl
to the ip address of your server, so http://1.2.3.4:3000
This is because localhost
when loaded from another computer is pointing to that computer.. but you need to make the socket connection back to the original server.
Upvotes: 3