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

Hi guys , Im trying to combine two libraries:

File::Tail and Net::WebSocket::Server. This what I have so far:

#!/usr/bin/perl use File::Tail; use Net::WebSocket::Server; my $file = File::Tail->new( name =>'./test.log', interval => 1, maxinterval => 1, ); Net::WebSocket::Server->new( listen => 8080, on_connect => sub { my ($serv, $conn) = @_; $conn->on( utf8 => sub { my ($conn, $msg) = @_; while (defined(my $line=$file->read)) { $conn->send_utf8('line :'.$line); } }, ); }, )->start;

my client test is:

 websocat ws://0.0.0.0:8080

But nothing is on the output.Any ideas ?

btw, doing:
tail -f test.log 2>&1 | websocat -s 8080 (on the server) and websocat ws://0.0.0.0:8080
it works.

Replies are listed 'Best First'.
Re: tail file and websocket stream
by jimpudar (Pilgrim) on Nov 03, 2018 at 18:43 UTC

    Apologies in advance for the tangential answer, but since I've done something very similar using Mojolicious I thought I'd show you how I did it there. I'll take a look at your code and see if I can figure out what's wrong with it in the meantime.

    The controller:

    package TailStream::Controller::Tail; use Mojo::Base 'Mojolicious::Controller'; use Mojo::Util qw(url_unescape); sub stream_tail { my $self = shift; my $encoded_filename = $self->stash('filename'); $self->inactivity_timeout(3600); my $filename = url_unescape $encoded_filename; die "Couldn't open $filename" unless -e -r -f $filename; my $pid = open my $fh, '-|', 'tail', '-n', '+1', '-F', $filename; die "Couldn't spawn: $!" unless defined $pid; my $stream = Mojo::IOLoop::Stream->new($fh); my $sid = Mojo::IOLoop->stream($stream); $self->on( finish => sub { Mojo::IOLoop->remove($sid) } ); $stream->on( read => sub { $self->send( { text => pop } ) } ); $stream->on( close => sub { kill KILL => $pid; close $fh } ); $stream->timeout(0); $stream->start; } 1;

    And the application:

    package TailStream; use Mojo::Base 'Mojolicious'; sub startup { my $self = shift; my $r = $self->routes; $r->websocket('/tail_ws/*filename')->to('tail#stream_tail'); } 1;

    πάντων χρημάτων μέτρον έστιν άνθρωπος.

Re: tail file and websocket stream
by jimpudar (Pilgrim) on Nov 03, 2018 at 19:24 UTC

    It does seem to be working for me:

    $ websocat ws://0.0.0.0:8080 line :hello line :world line :line 3 line :nice line :to line :meet line :you

    Were you expecting the log to be streamed right when you connect? The way you wrote your server, you need to send a utf8 message to the server to trigger the event. Note that I pressed enter after starting websocat.

    Hope this helps!

    Jim

    πάντων χρημάτων μέτρον έστιν άνθρωπος.

      Hi , thanks for your response. The file im tailing is /var/log/messages. When doing:
      tail -f /var/log/messages | websocat -s 172.20.4.210:8080
      Works good , as soon Is I connect, I begin receiving messages. Regards.
Re: tail file and websocket stream
by zentara (Cardinal) on Nov 03, 2018 at 20:43 UTC
Re: tail file and websocket stream
by ForgotPasswordAgain (Vicar) on Nov 03, 2018 at 14:40 UTC
    (First, you should use strict and warnings.) Try debugging them separately: try to get File::Tail to output something, and try Net::WebSocket::Server with a hardcoded string.
      Thanks , for the advice, they works properly in separated scripts.