Channels
Loco
support opening channels sockets by enable the channels
feature flag. Socket implementation is based on socketioxide
crate.
How To Configure Channels
Once the channels
feature is enabled, you need to implement the register_channels
hook in the Hooks
of the app.rs
file. The register_channels
function should return an AppChannels
instance, describing all the channels to be registered into Loco.
Creating Channel Code
Begin by creating a folder called channels
. In this folder, create an application.rs
file, ensuring it is included in the mod.rs
file. Inside application.rs
, define the following function:
use ;
use Value;
Next, register the on_connect
function in Loco routes. Navigate to app.rs
and add register_channels
to the Hooks
app. After registering the channel, go to the routes hook and add the AppChannels
instance to AppRouter
.
use crate channels;
;
For a simple example of a chat room implementation, refer to this link.