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

Hi everybody, I am trying to use WWW::Pusher. The idea is to send messages from server to browser. I used script provided in the docs but it does not work.

use WWW::Pusher; my $pusher = WWW::Pusher->new( auth_key => 'YOUR API KEY', secret => 'YOUR SECRET', app_id => 'YOUR APP ID', channel => 'test_channel' ); my $response = $pusher->trigger(event => 'my_event', data => 'Tes +t message'); my $sock_auth = $pusher->socket_auth('socket_auth_key');
The message is delivered but is "undefine". Looking more carefully I realized that message is not formatted in the proper way by WWW::Pusher.It should be formatted like this:{"message": "Test message"}. WWW::Pusher is sending only "Test Message". Looking at JavaScript console in Chrome I should have: "data":{"message":"Test message"} but I am getting only: "data":"Test message", so clearly problem with formatting the message by WWW::Pusher. So I started looking at Pusher.pm file, hoping to be able to modify it and make it to work. I tried insert : {"message": "Test message"} directly into Pusher.pm but still it is undefined. In Chrome console I am getting this: "data":"{\"message\": \"Test message\"}". So the question: how to format this message so it will be properly sent by WWW::Pusher ?? Thanks like always, Robert

Replies are listed 'Best First'.
Re: WWW::Pusher - does not send a message
by choroba (Cardinal) on Jun 14, 2015 at 06:44 UTC
    From your description of its behaviour and from the following part of the documentation
    data can also be hash/arrayref. There should be no need to JSON encode your data.

    it seems the module JSONizes the data structure for you. So, just specify a Perl structure:

    my $response = $pusher->trigger( event => 'my_event', data => { message => 'Test message' +}, );
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Thanks for help. No I understand what was the problem. Data was not correctly formatted in JSON. I corrected Pusher.pm module. It is not the most elegant way, but it works for me So far I checked simple data transfer, compex data - will do later.

      Corrected script: https://github.com/rkrasowski/pusher/

      Thanks like always for help Robert

        The source code of WWW::Pusher is on GitHub. You should have forked the repository and created a pull request to fix the problem.

        But I'm not sure you really fixed any issues. You replaced

        my $payload = to_json($args{data}, { allow_nonref => 1 });

        with

        my $message = "$args{data}"; my %rec_hash = ('message' => $message); my $payload = to_json (\%rec_hash, { allow_nonref => 1 });

        (with broken indentation). Which means, your version of the module can only send "message", while the original allows for any data to be sent (not knowing the module's purpose, I'm not sure it's desirable, though). The solution I provided does exactly the same.

        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ