pub trait Driver {
type Error: Error;
// Required method
fn emit(
&self,
channel: String,
data: Vec<u8>,
) -> impl Future<Output = Result<(), Self::Error>>;
}Expand description
The abstraction between the socketio emitter and the underlying system.
You must implement it for your specific
Adapter driver.
For a redis emitter you would implement the driver trait to emit events to a pubsub channel. The only requirement is that the driver must be able to emit data to specified channels.
§Example with the redis crate
use redis::{AsyncCommands, aio::MultiplexedConnection};
use socketioxide_emitter::{Driver, IoEmitter};
struct RedisConnection(MultiplexedConnection);
impl Driver for RedisConnection {
type Error = redis::RedisError;
async fn emit(&self, channel: String, data: Vec<u8>) -> Result<(), Self::Error> {
self.0
.clone()
.publish::<_, _, redis::Value>(channel, data)
.await?;
Ok(())
}
}§Example with the fred crate
use fred::{
clients::SubscriberClient,
prelude::{ClientLike, PubsubInterface},
};
use socketioxide_emitter::{Driver, IoEmitter};
struct FredConnection(SubscriberClient);
impl Driver for FredConnection {
type Error = fred::error::Error;
async fn emit(&self, channel: String, data: Vec<u8>) -> Result<(), Self::Error> {
self.0.publish::<u16, _, _>(channel, data).await?;
Ok(())
}
}Required Associated Types§
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.