Search code examples
javascriptnode.jswebsocketphantomjscasperjs

PhantomJS doesn't start while websocket connection is open


I'm working on slack bot and I've encountered curious problem. I have a module which scrapes web-page using phantomJS (via SpookyJS & CasperJS on top of it). I wrote this module and tested it running it from command line manually. It works well. But then I added slackbots npm module which abstracts Slack realtime API, and created a module with bot class. This bot module requires my module with scraping code (phantomJS) and calls its function when message event triggers:

var getAnswer = require('./getAnswer');

myBot.prototype._onMessage = function (message) {
    if (this._isChatMessage(message) &&
        this._isChannelConversation(message) &&
        this._isMentioningMe(message)) {

        this._reply(message);
    }
};

this._reply basically just calls getAnswer(originalMessage.text) and then self.postMessageToChannel(channel.name, reply, {as_user: true});

getAnswer returns a promise, but it never get's fulfilled. I made CasperJS be verbose and saw that nothing happens after

[info] [phantom] Starting...

Everything just hangs...

I have no idea, how to fix this. I guess it's because slackbots module establishes websocket connection when I call Bot.prototype.run. Any suggestions?


Solution

  • As I said I use Spooky to spawn child CasperJS process. I went to Spooky documentation page and read this:

    Specifically, each Spooky instance spawns a child Casper process that runs a bootstrap script. The bootstrap script sets up a JSON-RPC server that listens for commands from the parent Spooky instance over a transport (either HTTP or stdio). The script also sets up a JSON-RPC client that sends events to the parent Spooky instance via stdout.

    I used http as a transport and it didn't work, so I changed it to stdio and it helped. I'd appreciate it if someone could explain why it helped?