Fine Control of Ports in Chestnut

I have two requirements:

  1. Using Vagrant to get a “clone and go” experience for my team.
  2. Being able to dev multiple Figwheel apps on the same machine at once, which requires port juggling.

To do this, there needs to be some port forwarding magic.

Thus far, I have:

  1. The main REPL fixed to port 30000 (via project.clj) on the guest, which I use an env var on vagrant up in the host to dynamically set at VM launch time.
  2. The web server port 10555 on the guest (default) mapped via host env var at VM launch time for the browser to connect to.

Where I’m stuck is the Figwheel websocket.

  1. How do I fix the port so that I can tell Vagrant to map it to a fixed port on the host for the browser-process JS to connect to?
  2. What is the easiest way to allow the fixed host port to vary between VM launches? (I can do this with host env vars for vagrant up, but this wouldn’t propagate into the guest OS at lein * call.)

Thanks!
Alyssa

I’d personally look into accessing the vm through other means other than port forwarding by way of using static IPs of the VMs and naming these through a hostfile (or similar dns mapping)

If you still wanted to configure different ports for you apps, you have the :server-port figwheel option (defaulting to 3449). Each app could have its unique port, so the port config would be passthrough.

OK. I take it then that using environment variables, or having project.clj read from a file, is architecturally impossible? Just looking for confirmation.

The reason I ask is because it would be nice, in the case of working with two instances of the same project twice (on different branches), not to have to edit the project.clj file in one of them, and risk accidentally checking the change in. Not really that big of a deal, I know.

Hmm. I tried configuring the private network in Vagrant, which worked, but then in project.clj set :websocket-host :js-client-host inside of :cljsbuild / :builds / :figwheel for :id "app". I’m looking at the JS console in Chrome, and although window.location.hostname returns the correct private network IP for the Vagrant guest, Figwheel is insisting on trying to connect to localhost.

Holy crap. Had to lein clean to get the :websocket-host :js-client-host change to take effect. Disregard! Thanks!

1 Like

glad you figured it out - when in doubt, lein clean:wink:

You can read environment variables or files in project.clj just fine, the trick is to prefix any code that needs to be evaluated with ~.

(defproject foo "0.0.1"
  :some-port ~(java.lang.System/getenv "SOME_PORT"))
2 Likes

When I tried that, Figwheel complained that the ~(System/getenv "PORT") form wasn’t an Integer.

Right, environment variables are always strings, so in this case you need to make sure to convert it to an integer, something like ~(Integer/parseInt (System/getenv "PORT")).

OMG I’ll try it when I get hands on keyboard. I hope it wasn’t something that simple… I got thrown off by the error message because it seemed like it wasn’t evaluating the escaped form at all.

OK, confirmed. I can’t believe I was that thrown off by the error message. Really closing out now.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.