0

I'm working on a socket app in node.js, I'm using it to make a remote controller.

I need to know in which IP the server is running so Socket.io can connect to it. I have already done it in the app using node.js but now I need to pass it to the html page where the server is running.

This is my javascript on the html page:

<script type="text/javascript">
var socket = io.connect('http://' + /*IP*/ + ':3340', { 'forceNew': true });
function addMessage(e) {
  socket.emit('new-message', e);
  return false;
}
</script>

Is there any way I can check in which address is the server runnig only using javascript on the html page?

Localhost doesn't work since I want to access it from my mobile or another computer, I can't access to the .js file where I got the IP before and I want it to be as automated as possible so the user has only to connect to an url and not be looking for his local IP.

Thank so much.

Albermonte
  • 31
  • 5
  • Local address for a LAN IP: https://stackoverflow.com/questions/3653065/get-local-ip-address-in-node-js, for the Public IP address you need to ask something external: https://stackoverflow.com/questions/20273128/how-to-get-my-external-ip-address-with-node-js – Alex K. Dec 13 '17 at 17:51
  • `let socket = io();` connects to my own domain. `let socket = io(':3340');` to change port. Probably similar with the connect function. – Mika Sundland Dec 13 '17 at 17:53
  • @MikaS Awesome, it works. Thanks so much. – Albermonte Dec 13 '17 at 18:04
  • Is the socket.io server the same server as the web page comes from? If so, you can use do `var socket = io();` in the web page and socket.io will automatically use the host from the web page URL. – jfriend00 Dec 13 '17 at 18:24

0 Answers0