lydell/elm-watch

Ability to set domain for websocket server

Opened this issue · 3 comments

I can tell the whole thing is intended to run on localhost, and that works well.

However I had reason to want to run the script in production, using a local development server to serve the script. To test and figure out what was wrong of course, it enabled me to add Debug.log and similar to figure out what the problems were.

However the websocket insisted on running on the public domain, the same as the page was served from. I suspect it just uses a relative url.

I could do it just by using make instead of watch, but if I could have set the full domain I would have had all the power of the tool.

lydell commented

Hi! For now, you can use the hack in #46 (comment) to customize the URL.

lydell commented

If you manage to get the hack working, it would be helpful if you could share what you ended up with here. Helps me understand what potential support for this could look like.

Yes, it was easy to get the hack working:

<script>
  console.log("Hacking WebSocket to make it work off site in dev mode.")
  window.WebSocket = class HackWebSocket extends WebSocket {
    constructor(urlParam) {
      console.log("Hacking WebSocket...")
      const url = new URL(urlParam)
      if (looksLikeElmWatch(url)) {
        console.log("this looks like elm-watch, hacking url...")
        url.hostname = "testsajt-ulrik.wm3.test"
      }
      super(url)
    }
  }

  function looksLikeElmWatch(url) {
    return url.port === "44331";
    // or return url.pathname === "/elm-watch";
  }
</script>

Just changing the hostname instead of the port from the example.

As an aside, I use https for local development, which makes it easy since obviously production is https. Don't know if it could have worked with different protocols.

PS: obviously "testsajt-ulrik.wm3.test" is an alias for localhost I've set up.