There should be a way to subscribe to data updates using the Yancy::Controller::Yancy. Add the following actions to the controller:
get_ws - Get a WebSocket for updates to a single item. Every message will be an update to the given item, which may be the entire item or only those fields which have changed.
Required configuration:
collection - The collection to work with
id - The ID of the item to get updates from
list_ws - Get a WebSocket for updates to an entire collection.
Required configuration:
collection - The collection to work with
Future enhancements can include filtering which items will get sent
Every update message is a JSON object. The object's keys are item IDs, and the values are either null (if the item has been deleted) or the item changes (which may be the entire item).
The set, create, and delete actions should send a message to any connected WebSockets. Create a new plugin, Yancy::Plugin::Notify available from the helper yancy.notify. This plugin should have three methods:
subscribe - Used by get_ws and list_ws to subscribe to updates. Takes two arguments: The collection name to subscribe to, and a subref to call to send the message. This subref receives two arguments, the ID of the item and the item to send. The subref decides whether to send the message, so get_ws only sends messages for the one item it wants. Returns the subref given, for later use to unsubscribe.
unsubscribe - Used by get_ws and list_ws to unsubscribe from updates. Takes two arguments: The collection name to unsubscribe from, and the subref used to subscribe.
send - Used by set, create, and delete to send updates for an item. Takes three arguments: The collection name, the item ID, and the item data (or undef if the item has been deleted).
In the future, Yancy::Plugin::Notify can support message broker backends like Mercury, Redis, Mojo::Pg, etc... to coordinate messages between forked / distributed daemons.
The feed endpoint in Yancy::Controller::Yancy has existed for a while, and serves this need. It is polling-based, which is suboptimal, but optimizations can come later if needed.
There should be a way to subscribe to data updates using the Yancy::Controller::Yancy. Add the following actions to the controller:
get_ws
- Get a WebSocket for updates to a single item. Every message will be an update to the given item, which may be the entire item or only those fields which have changed.collection
- The collection to work withid
- The ID of the item to get updates fromlist_ws
- Get a WebSocket for updates to an entire collection.collection
- The collection to work withEvery update message is a JSON object. The object's keys are item IDs, and the values are either
null
(if the item has been deleted) or the item changes (which may be the entire item).The
set
,create
, anddelete
actions should send a message to any connected WebSockets. Create a new plugin,Yancy::Plugin::Notify
available from the helperyancy.notify
. This plugin should have three methods:subscribe
- Used byget_ws
andlist_ws
to subscribe to updates. Takes two arguments: The collection name to subscribe to, and a subref to call to send the message. This subref receives two arguments, the ID of the item and the item to send. The subref decides whether to send the message, soget_ws
only sends messages for the one item it wants. Returns the subref given, for later use to unsubscribe.unsubscribe
- Used byget_ws
andlist_ws
to unsubscribe from updates. Takes two arguments: The collection name to unsubscribe from, and the subref used to subscribe.send
- Used byset
,create
, anddelete
to send updates for an item. Takes three arguments: The collection name, the item ID, and the item data (or undef if the item has been deleted).In the future, Yancy::Plugin::Notify can support message broker backends like Mercury, Redis, Mojo::Pg, etc... to coordinate messages between forked / distributed daemons.