Streaming interface to the PhantomJS headless browser


Write and automate PhantomJS scripts inside node, using a standard Stream interface.

Getting Started

First off, install the phantomstream module.

npm install phantomstream

Next, create a simple script named "myscript.js".

phantomstream = require("../phantomstream")
// Open up a PhantomJS stream that listens for commands that look like 
// "TITLE <url>". Browse to the URL and write the title back via the stream. 
var conf = {logger: console}
var ps =, function(nodestreamphantomrequireglobal) {
  // This callback executes inside of PhantomJS, not node.  To see the API 
  // available to you in PhantomJS, check out their documentation: 
  var page = require("webpage").create()
  // Listen for the "TITLE <url>" commands via the streaming interface. 
  nodestream.on("data", function(data) {
    var matches = /TITLE\s*(.+)/.exec(data)
    if (matches) {
      var url = matches[1]
      // Got a TITLE command, use the PhantomJS page object to browse 
      // to that page, get the document title, and write it back to the stream."opening", url), function() {
        var title = page.evaluate(function() {
          return document.title
        })"sending title =", title)
// Send an TITLE command to our PhantomJS process and echo the response. 
ps.on("data", function(data) {"node received:", data.toString())

Run your script with node.

node myscript.js should see output like this:

[phantom-stdout] opening
[phantom-stdout] sending title = Google
node received: Google

Enjoy! For more ideas on what is possible inside the callback for, read the PhantomJS documentation.

Don't forget try out some demos from the examples/ directory :)


If you are looking for higher-level implementations that wrap the PhantomJS API, check these out: