Search code examples
javascriptnode.jslocalhost

Is it possible to change the host for a localhost server?


Lets say I want to have a friend run my project that runs web sockets and a few other local servers on my network so me and my other friends can test my project. If the friend who hosted the web sockets had to leave, he would have to close the web sockets, and that would throw things off.

I have a few other servers running that each client is hosting separately (Vite and Express, but they don't have any part in this), and the web pages in each server are connecting to the web sockets from that one host. So as you can see, the host disconnecting is an issue. My other servers run off of those web sockets.

Am I able to have multiple hosts (where the next host takes care of all the requests and socket messages if the current host is gone), or have something transfer the hosting to the next person (The person who connected after the host before disconnection)?


Solution

  • In short, the answer is yes.

    Properly provisioning a corner of the internet (your own DNS domain, paying for a hosting, etc.) is well covered by others. This answer is an alternative approach, which can be done, with some context and limitations.

    For your scenario, there's probably a couple features that need to be implemented, like putting re-connection logic in the app. Basically if the server you connect to disappears, your app should be trying to find on the network again, and then return to normal behavior, rather than quitting on the first returned error.

    Other problems might be better handled in the network configuration of the web-app and its dependencies.

    This might be an extreme example solution, but some people have projects that are using a couple of randomly selected (but fixed IP) addresses in the 17.0.0.0 subnet, which is the Class A private network address space. The chances that a truly-randomly selected set of 4 address would collide in a class A subnet are pretty low. But don't get half-clever and think: nobody else would ever do this on the 17 network, so I'm going to use 17.0.0.1 and 17.0.0.2.