Search code examples
node.jsexpressclojureclojurescriptfigwheel

Running FIgwheel with Express.JS


Running lein figwheel, from my understanding, starts up a simple static hosting server (via ring) behind the scenes at port 3449. This works on my system.

The problem is that I am hosting my own files via node.js at port 3000. When I run figwheel, and then start my express.js server, changing my front-end files bizarrely affects both ports the same (i.e., updating a file causes both pages to re-render immediately).

Question: But how is this possible, if figwheel doesn't know anything about the express.js port 3000 server? And, in any event, is this the best way to use figwheel with my own express server (i.e., have two instances running, one default at 3449, and my own port at 3000)?


Solution

  • The page is running javascript built from ClojureScript. The build includes figwheel client code. The code that comes from the figwheel client is opening a websocket connection to a predefined location. It doesn't matter where the page is hosted, the javascript will try to connect to the predefined websocket endpoint. So if you load the page from the server on 3000, the websocket is still established to the server process you created with lein figwheel. The important bit is that there is the figwheel client code, and the figwheel server. The figwheel server is watching the filesystem and telling any websocket connections when the files change. The figwheel client is just connecting to a known websocket and listening for instructions to reload artifacts. So you don't need to use figwheel to serve your static content, as you already discovered.

    As for "is this the best way?" Well it is entirely dependent on your goals. Certainly it works great, so if there is some advantage to running both, go for it! On the other hand, why run an express server if you don't need it?

    One circumstance where running 2 servers is advantageous is when you are relying on server side functionality that does not fit well with using figwheel as the host. This could be due to deployment (maybe you don't want to use a ring handler as your primary server), or a technical limitation (I like to use httpkit for websockets, and afaik there isn't a good way of running these from a ring handler). But, as you pointed out, you can still use figwheel for fast development reloading even if you aren't using it to serve files or services.

    Obviously the figwheel server needs to be watching the right files though, so make sure figwheel is configured to observe the right directories.