Struct jsonrpc_utils::pub_sub::PublishMsg
source · pub struct PublishMsg<T> { /* private fields */ }Available on crate feature
server only.Expand description
Inner message published to subscribers.
Implementations§
source§impl<T: Serialize> PublishMsg<T>
impl<T: Serialize> PublishMsg<T>
sourcepub fn result(value: &T) -> Self
pub fn result(value: &T) -> Self
Create a new “result” message by serializing the value into JSON.
If serialization fails, an “error” message is created returned instead.
Examples found in repository?
examples/broadcast.rs (line 27)
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
async fn main() {
let mut rpc = MetaIoHandler::with_compatibility(jsonrpc_core::Compatibility::V2);
let (tx, _) = broadcast::channel(8);
tokio::spawn({
let tx = tx.clone();
async move {
for i in 0u64.. {
// Error can be ignored.
//
// It is recommended to broadcast already serialized
// `PublishMsg`. This way it only need to serialized once.
drop(tx.send(PublishMsg::result(&i)));
tokio::time::sleep(Duration::from_secs(1)).await;
}
}
});
add_pub_sub(
&mut rpc,
"subscribe",
"subscription",
"unsubscribe",
move |_params: Params| {
Ok(BroadcastStream::new(tx.subscribe()).map(|result| {
result.unwrap_or_else(|_| {
PublishMsg::error(&jsonrpc_core::Error {
code: jsonrpc_core::ErrorCode::ServerError(-32000),
message: "lagged".into(),
data: None,
})
})
}))
},
);
let rpc = Arc::new(rpc);
let config = StreamServerConfig::default()
.with_channel_size(4)
.with_pipeline_size(4)
.with_keep_alive(true);
let app = jsonrpc_router("/rpc", rpc.clone(), config);
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}source§impl<T> PublishMsg<T>
impl<T> PublishMsg<T>
sourcepub fn error(err: &Error) -> Self
pub fn error(err: &Error) -> Self
Create a new “error” message by serializing the JSONRPC error object.
Panics
If serializing the error fails.
Examples found in repository?
examples/broadcast.rs (lines 40-44)
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
async fn main() {
let mut rpc = MetaIoHandler::with_compatibility(jsonrpc_core::Compatibility::V2);
let (tx, _) = broadcast::channel(8);
tokio::spawn({
let tx = tx.clone();
async move {
for i in 0u64.. {
// Error can be ignored.
//
// It is recommended to broadcast already serialized
// `PublishMsg`. This way it only need to serialized once.
drop(tx.send(PublishMsg::result(&i)));
tokio::time::sleep(Duration::from_secs(1)).await;
}
}
});
add_pub_sub(
&mut rpc,
"subscribe",
"subscription",
"unsubscribe",
move |_params: Params| {
Ok(BroadcastStream::new(tx.subscribe()).map(|result| {
result.unwrap_or_else(|_| {
PublishMsg::error(&jsonrpc_core::Error {
code: jsonrpc_core::ErrorCode::ServerError(-32000),
message: "lagged".into(),
data: None,
})
})
}))
},
);
let rpc = Arc::new(rpc);
let config = StreamServerConfig::default()
.with_channel_size(4)
.with_pipeline_size(4)
.with_keep_alive(true);
let app = jsonrpc_router("/rpc", rpc.clone(), config);
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}sourcepub fn result_raw_json(value: impl Into<Arc<str>>) -> Self
pub fn result_raw_json(value: impl Into<Arc<str>>) -> Self
Create a new “result” message.
value must be valid JSON.
sourcepub fn error_raw_json(value: impl Into<Arc<str>>) -> Self
pub fn error_raw_json(value: impl Into<Arc<str>>) -> Self
Create a new “error” message.
value must be valid JSON.
Trait Implementations§
source§impl<T: Clone> Clone for PublishMsg<T>
impl<T: Clone> Clone for PublishMsg<T>
source§fn clone(&self) -> PublishMsg<T>
fn clone(&self) -> PublishMsg<T>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read more