2

Here is my issue : I have got an API using mojolicious, an external script perl and a JS file, and I would like to connect them this way: the external script launch a random POST request, if it is a success it have to send the message "Success" throught a websocket. If an error occures, It will have to send "Error". The websocket on the API will just relay the message for the JS which will use it.

How I imagine the code to be :

Inside the Mojolicious launcher script:

websocket '/foo' => sub {
 $self->on(message => sub {
  my ($self, $msg) = @_;
  $self->send($msg);
 });
};

when get a message send it

Inside the JS file:

var ws = new WebSocket('ws://api/foo'); 
ws.onmessage = function(msg){
 if(msg == "Error") {console.log("got an error")};
 else if(msg == "Success") {console.log("got a success")};
};

So, how can I connect my external script to the websocket, and be able to send "Error" or "Success"? (This external script has nothing to do with the web server, it s somewhere else, doing something else).

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
artless boy
  • 431
  • 1
  • 5
  • 11
  • https://github.com/kraih/mojo/wiki/Writing-websocket-chat-using-Mojolicious-Lite <-- – mpapec Aug 24 '17 at 17:58
  • I used this link to write this code (and I have a minor error on the JS), but that s not exactly what I am looking for, thx anyway :) – artless boy Aug 24 '17 at 18:03
  • If someone ever arrived here, you may can find a solution in question asked here https://stackoverflow.com/questions/37186906/why-websocket-connections-breaksn not really what I was seeking but it can't be unseen if you have this issue – artless boy Aug 25 '17 at 08:21

1 Answers1

0

I understand that you want a plain perl-script who connects to the Mojolicious webserver where you have a websocket endpoint. The external perl script should connect to the websocket server and send some messages. The websocket server can then redistribute those messages to other clients.

Check my github where you can find the above screnario.

Perl Mojolicious websockets

Rob Lassche
  • 841
  • 10
  • 16