Dancer2-Plugin-WebSocket

 view release on metacpan or  search on metacpan

README.mkdn  view on Meta::CPAN


`MyApp.pm`:

```perl
package MyApp;

use Dancer2;
use Dancer2::Plugin::WebSocket;

websocket_on_message sub {
  my( $conn, $message ) = @_;
  $conn->send( $message . ' world!' );
};

get '/' => sub {
  my $ws_url = websocket_url;
  return <<"END";
    <html>
      <head><script>
          var urlMySocket = "$ws_url";

          var mySocket = new WebSocket(urlMySocket);

          mySocket.onmessage = function (evt) {
            console.log( "Got message " + evt.data );
          };

          mySocket.onopen = function(evt) {
            console.log("opening");
            setTimeout( function() {
              mySocket.send('hello'); }, 2000 );
          };

    </script></head>
    <body><h1>WebSocket client</h1></body>
  </html>
END
};

get '/say_hi' => sub {
  $_->send([ "Hello!" ]) for websocket_connections;
};

true;
```

# DESCRIPTION

`Dancer2::Plugin::WebSocket` provides an interface to [Plack::App::WebSocket](https://metacpan.org/pod/Plack::App::WebSocket)
and allows to interact with the webSocket connections within the Dancer app.

[Plack::App::WebSocket](https://metacpan.org/pod/Plack::App::WebSocket), and thus this plugin, requires a plack server that
supports the psgi _streaming_, _nonblocking_ and _io_. [Twiggy](https://metacpan.org/pod/Twiggy)
is the most popular server fitting the bill.

# CONFIGURATION

- serializer

    If serializer is set to a `true` value, messages will be assumed to be JSON
    objects and will be automatically encoded/decoded using a [JSON::MaybeXS](https://metacpan.org/pod/JSON::MaybeXS)
    serializer.  If the value of `serializer` is a hash, it'll be passed as
    arguments to the [JSON::MaybeXS](https://metacpan.org/pod/JSON::MaybeXS) constructor.

    ```
    plugins:
        WebSocket:
            serializer:
                utf8:         1
                allow_nonref: 1
    ```

    By the way, if you want the connection to automatically serialize data
    structures to JSON on the client side, you can do something like

    ```perl
    var mySocket = new WebSocket(urlMySocket);
    mySocket.sendJSON = function(message) {
        return this.send(JSON.stringify(message))
    };

    // then later...
    mySocket.sendJSON({ whoa: "auto-serialization ftw!" });
    ```

- mount\_path

    Path for the websocket mountpoint. Defaults to `/ws`.

# PLUGIN KEYWORDS

In the various callbacks, the connection object `$conn`
is a [Plack::App::WebSocket::Connection](https://metacpan.org/pod/Plack::App::WebSocket::Connection) object
augmented with the [Dancer2::Plugin::WebSocket::Connection](https://metacpan.org/pod/Dancer2::Plugin::WebSocket::Connection) role.

## websocket\_on\_open sub { ... }

```perl
websocket_on_open sub {
    my( $conn, $env ) = @_;
    ...;
};
```

Code invoked when a new socket is opened. Gets the new
connection
object and the Plack
`$env` hash as arguments.

## websocket\_on\_login sub { ... }

```perl
websocket_on_login sub {
    my( $conn, $env ) = @_;
    ...;
};
```

Code invoked when a new socket is opened. Gets the
connection object and the Plack `$env` hash as arguments.



( run in 1.166 second using v1.01-cache-2.11-cpan-39bf76dae61 )