Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've an Mojo Useragent to connect a system to do regular tasks, and it works fine by far. Recently our R&D team introduce Stomp to do some long-live stuff. Since they use Java & JS, I have to implement it in perl by myself. Below is some example code in JS:
var url = "ws://localhost:61614/stomp"; var client = Stomp.client(url); function afterConnect(roomid) { btn.addEventListener('click', function () { var msg = input.value; client.send(roomid, {}, msg); }, false); } function createConnect(roomid, uid) { client.connect(headers, function (error) { if (error.command == "ERROR") { console.error(error.headers.message); } else { afterConnect(roomid); client.subscribe(uid, function (msg) { var body = msg.body; if (msg.headers['content-type'] == 'application/json') { body = JSON.parse(msg.body) } }); } }); } ...... ......
The logic is simple, a local stomp client connect to server over websocket, then get data, modify data save data etc. What I confuse is, I've searched Stomp on cpan, all modules about Stomp does not support WS, The Mojo::ua support WS (and async which why I prefer it), but doesn't support Stomp Protocol directly. Before I dig into Stomp protocol, is there a decent way to combine Mojo::ua and Stomp(Like Net::Stomp)? Thanks.

Replies are listed 'Best First'.
Re: how Mojo::ua work with Net::Stomp
by Corion (Patriarch) on Oct 14, 2023 at 09:37 UTC

    In the end you will have to learn about the Stomp payloads you will be using. The Javascript sends some string msg:

    client.send(roomid, {}, msg);

    Net::Stomp also sends some string with ->send.

    With Mojo::UserAgent this would likely look like the following, as taken straight from the documentation:

    $ua->start($tx => sub ($ua, $tx) { say 'WebSocket handshake failed!' and return unless $tx->is_websoc +ket; $tx->on(message => sub ($tx, $msg) { say "WebSocket message: $msg"; $tx->finish; }); $tx->send('some msg'); });
      Thanks Corion, you are right, but send is a low level command on stomp, I think, Before I reinvent wheel, I’d like to find a module to encapsulate usual methods like subscribe, unsubscribe, disconnet etc and work with mojo::ua.