pub struct KrakenWSSClient<'a> { /* private fields */ }
Expand description
A client for connecting to Kraken websockets.
Connecting to the public (connect
) or private (connect_auth
) uris returns a KrakenMessageStream
that can be awaited for values after sending subscriptions or other messages.
ยงExample: Listening to OHLC/Candles
Creating a client for public messages requires no token or authentication. All thatโs needed is to send a valid subscription message for some number of pairs to subscribe to, then listen indefinitely.
use kraken_async_rs::wss::v2::base_messages::{Message, WssMessage};
use kraken_async_rs::wss::v2::kraken_wss_client::KrakenWSSClient;
use kraken_async_rs::wss::v2::market_data_messages::OhlcSubscription;
use std::time::Duration;
use tokio::time::timeout;
use tokio_stream::StreamExt;
#[tokio::main]
async fn main() {
let mut client = KrakenWSSClient::new();
let mut kraken_stream = client.connect::<WssMessage>().await.unwrap();
let ohlc_params = OhlcSubscription::new(vec!["ETH/USD".into()], 60);
let subscription = Message::new_subscription(ohlc_params, 0);
let result = kraken_stream.send(&subscription).await;
assert!(result.is_ok());
while let Ok(Some(message)) = timeout(Duration::from_secs(10), kraken_stream.next()).await {
if let Ok(response) = message {
println!("{:?}", response);
} else {
println!("Message failed: {:?}", message);
}
}
}
Implementationsยง
Sourceยงimpl<'a> KrakenWSSClient<'a>
impl<'a> KrakenWSSClient<'a>
Sourcepub fn new() -> KrakenWSSClient<'a>
pub fn new() -> KrakenWSSClient<'a>
Create a client using the default Kraken URLs.
Sourcepub fn new_with_urls(
base_url: &'a str,
auth_url: &'a str,
) -> KrakenWSSClient<'a>
pub fn new_with_urls( base_url: &'a str, auth_url: &'a str, ) -> KrakenWSSClient<'a>
Create a client with custom URLs.
This is most useful for use with a proxy, or for testing.
Sourcepub async fn connect(
&mut self,
) -> Result<KrakenMessageStream<PublicMessage>, WSSError>
pub async fn connect( &mut self, ) -> Result<KrakenMessageStream<PublicMessage>, WSSError>
Connect to the Kraken public websocket channel, returning a Result
containing a
KrakenMessageStream
of PublicMessage
s.
Sourcepub async fn connect_auth(
&mut self,
) -> Result<KrakenMessageStream<PrivateMessage>, WSSError>
pub async fn connect_auth( &mut self, ) -> Result<KrakenMessageStream<PrivateMessage>, WSSError>
Connect to the Kraken private websocket channel, returning a Result
containing a
KrakenMessageStream
of PrivateMessage
s.
Trait Implementationsยง
Sourceยงimpl<'a> Clone for KrakenWSSClient<'a>
impl<'a> Clone for KrakenWSSClient<'a>
Sourceยงfn clone(&self) -> KrakenWSSClient<'a>
fn clone(&self) -> KrakenWSSClient<'a>
1.0.0 ยท Sourceยงfn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more