UP | HOME

Using a random port with phantomjs' webserver module

I started building an erlang wrapper for phantomjs, as a stepping stone to clustering (and babysitting) phantomjs instances behind an API. While researching this problem I bumped into the node-phantom-simple project on github. It uses the phantomjs webserver module to communicate between node and phantomjs. Typically, you would assign your phantomjs webservers port 0, to atomically get a random unused port. Since phantomjs webserver module returns 0 instead of the port the socket is bound to, the author of the node-phantom-simple project resorted to some questionable methods to get a random port.

I wrote a patch for phantomjs 2.1.1 which corrects this. This won't be merged since the webserver module will be replaced entirely, so I thought I'd leave it here for anyone that wants to patch older versions until its fixed. Just follow the build instructions and apply the patch before invoking build.py.

../attachments/0001-Make-reading-server.port-return-the-correct-value-wh.patch