Is there any way to send back result via websocket without waiting all done

47 views
Skip to first unread message

Peter L

unread,
Jan 18, 2019, 12:30:07 AM1/18/19
to Mojolicious
Hi,

I have a network equipment which is similar to the Cisco L3 switch. I use mojo to setup a web server on a Linux PC. The browser can send some CLI commands via websocket to web server. The web server can run these commands on network equipment and send all the results back to the browser via websocket. It works. But, It need to take some time to wait for all results done. In this case, if I have many CLI commands, it will take a long time to wait all results done. I want to send the result back to the browser if one command is executed without waiting for the other commands executing. Is there any way to do it via websocket?



websocket "/runcmd" => sub {

    $c->shift;

    ...

    $c->on(message => sub {
         my ($c, @commands) = @_;

         foreach ( @commands) {

              # back-end to run commands via ssh/telnet session
              my $ret = backend( $_);

              $c->send( encode_json { result => $ret});
         }

    });
   
    ...
}

Thanks,
Peter

Stefan Adams

unread,
Jan 18, 2019, 8:51:29 AM1/18/19
to mojolicious
Hi!

It seems to me that you should structure your code such that each command emits a 'message' event; it appears that you have a single routine which runs all of your commands and then emits a single message for all of the output.  Impossible for me to say of course, but that's what I get out of what I see from your snippet.

Alternatively, backend() needs to be non-blocking and there's a lot behind the scenes there.

Sorry that's not any help, but I can at least reassure you that what you are driving towards is definitely possible!  You can definitely send the result back to the browser via websocket of just one command without waiting for the others to finish.

This sounds like a project I'd be interested in contributing to if you can make your code public.  I've worked on similar things in the past, specifically for monitoring Linux boxes.

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Luc Larochelle

unread,
May 6, 2019, 2:29:49 PM5/6/19
to Mojolicious
I'm thinking about building the same type of system. To be very honest, I thought about websockets but wouldn't know where to start.

In a homemade config backup system, I decided to migrate the configuration file to a nice CRUD frontend built with Mojolicious + AngularJS + Bootstrap. As a nice-to-have feature, I thought that each time a user adds a new element to the configuration (e.g.: a new host to backup) , it could deploy a backend job to test a simple command such as "show version" on the node and that the result could be sent to a websocket so that a status light indicator could be displayed on the node's line. (in a table). 

Anyways, I'm very interested in that project, please share your success !

Cheers,

Luc
Reply all
Reply to author
Forward
0 new messages