- BUNDLED FILES
- SOURCE REPOSITORY
- ADDITIONAL THANKS
- COPYRIGHT AND LICENSE
Mojolicious::Plugin::Multiplex - A websocket multiplexing layer for Mojolicious applications
Their proposal only includes a partially implemented reference implementation. This module extends the protocol slightly in order to enough of the "WebSocket API" to be useful. More extensions may be necessary if the API is to be completely implemented, however those last few details are rarely used and will likely not be missed.
On the server-side the logic is entirely up to the application author. The module simply parses the multiplexed messages and emits events in accordance with them. A typical use case may be to relay message to a bus, subscribing and unsubscribing from topics that it presents. Another might be to stream updates to multiple types of data (perhaps in multiple parts of a single page application). (Indeed those might not be distinct cases from each other).
my $multiplex = $c->multiplex;
Establishes the WebSocket connection (if it hasn't been already) and returns an instance of Mojolicious::Plugin::Multiplex::Multiplexer. The multiplexer is attached to the websocket stream and begins listening for messages. The multiplexer emits events for incoming messages and has methods to send outgoing messages; more details about those are contained in its own documentation.
Note that for each websocket connection the same instance of the multiplexer will be returned on any subsequent call. Though not prevented, the user is highly discouraged from sending other traffic over any websocket connection that is managed by a multiplexer.
<script type="module"> import WebSocketMultiplex from '/websocket_multiplex.mjs'; var ws = new WebSocket(url); var multiplex = new WebSocketMultiplex(ws); var channel = multiplex.channel(topic); </script>
websocket_multiplex.mjs which contains the front-end code to create a multiplexer. It exports the
WebSocketMultiplex class, whose constructor takes as its only argument an existing WebSocket object or a url string to build one. This then is used to open new channel objects via the
channel method which takes a topic string as an arugment. Topics can be almost any string, however they must not contain a comma (a limitation of the protocol). The resulting channel objects implement the same API as a WebSocket (though they do not inherit from it).
The client-side multiplexer will also attempt to reconnect to closed sockets and when successful will automatically resubscribe to the channels that were subscribed.
N.B. This library is the least stable of the entire project. Use with caution.
Also, this library will likely use very modern conventions, even going forward. Older browsers are not the target for this file. For those you want ...
<script src="websocket_multiplex.js"></script> <script> var ws = new WebSocket(url); var multiplex = new WebSocketMultiplex(ws); var channel = multiplex.channel(topic); </script>
WebSocketMultiplex when loaded. In all other ways it works just like the above file.
Joel Berger, <firstname.lastname@example.org>
Copyright (C) 2016-2018 by Joel Berger
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.