title: Unable to connect to an external websocket from inside a Mojolicious route handler
body: <p>I recently found myself dabbling with Mojolicious in order to accomplish  what I initially believed to be a simple task.</p>
  
  <p>I am interfacing with some VoIP devices which can interact with external web servers, albeit in a pretty limited and convoluted fashion.</p>
  
  <p>My wish is to publish information about the current caller to the VoIP phones extracting data from our customers database.
  Unfortunately that's not as straightforward as one might hope.
  The phone should GET a web page on my web application, which in turn should query the phone for information on its calling status. That information will be POSTed by the phone to a fixed path on the web server. That calling information would be used to extract additional data from a database, through which render the response to the first GET by the phone.
  Overly complicated but the only way I can think of given the limitations of the devices.</p>
  
  <p>And here we come to my woes with Mojolicious. I must forewarn that I am a sysadmin by trade and usually my coding doesn't touch server processes, especially asynchronous/non-blocking programming.</p>
  
  <p>After a day of experimentation I came to the conclusion that for the two route handlers (correct terminology?), the one queried by the phone initially and the one to which the phone POSTs its calling status, to communicate I would need some kind of message broker. I opted for Mercury using websockets, since I didn't want to deploy a database system just for this purpose.</p>
  
  <p>I have no problem performing normal POST/GET requests from inside a route handler, but when I try non-blocking websockets for pub/sub purposes, I get a 'Premature connection close' and from further debugging I see that the connection to the external websocket server does not take place at all.</p>
  
  <pre class="lang-perl prettyprint-override"><code>post '/callinfo' =&gt; sub {
    my $c = shift;
    $c-&gt;inactivity_timeout(0); 
    my $ua = Mojo::UserAgent-&gt;new;
    my $remote_url = 'ws://127.0.0.1:4000/pub/'. $c-&gt;tx-&gt;remote_address;
    $ua-&gt;websocket( $remote_url =&gt; ['v2.proto', 'v1.proto'] =&gt; sub {
            my ($ua, $tx) = @_;
            say 'WebSocket handshake failed!' and return unless $tx-&gt;is_websocket;
            $tx-&gt;on(finish =&gt; sub {
                    my ($tx, $code, $reason) = @_;
                    say "WebSocket closed with status $code.";
                });
            $tx-&gt;on(message =&gt; sub {
                    my ($tx, $msg) = @_;
                    say "WebSocket message: $msg";
                    $tx-&gt;finish;
                });
            $tx-&gt;send('Hi!');
        }
    );
  }
  </code></pre>
  
  <p>The code above is my route handler, at this point pretty much boilerplate code from the Mojolicious documentation.</p>
  
  <p>I freely confess being pretty out of my element here, so I cannot figure out exactly why things are not going as I expect. I enabled all the debugging options I could find but I guess the problem must be some fundamental lack of comprehension of how the whole things works.</p>
  
  <p>What's more confounding is that in the other handler I have a similar websocket connection which comes after (although they are both non-blocking so I don't know if there's much meaning in the order) an HTTP GET call. And there it works. But if I comment the call performing the GET I get the same error with no actual connection taking place.</p>
  
  <p>So... I am stumped. Does anyone have an idea on the cause and/or what I could do to circumvent the issue?</p>
  
  <p>Feel free to correct me on terminology and core misunderstandings. This is my first foray in this kind of programming and with Mojolicious in particular.
  Unfortunately I cannot find enough projects or actual code around to take inspiration from and documentation is somewhat scarce.</p>