Struct convex::ConvexClient

source ·
pub struct ConvexClient { /* private fields */ }
Expand description

An asynchronous client to interact with a specific project to perform mutations and manage query subscriptions using tokio.

The Convex client requires a deployment url, which can be found in the dashboard settings tab.

use convex::ConvexClient;
use futures::StreamExt;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let mut client = ConvexClient::new("https://cool-music-123.convex.cloud").await?;
    let mut sub = client.subscribe("listMessages", maplit::btreemap!{}).await?;
    while let Some(result) = sub.next().await {
        println!("{result:?}");
    }
    Ok(())
}

The ConvexClient internally holds a connection and a tokio background task to manage it. It is advised that you create one and reuse it. You can safely clone with ConvexClient::clone() to share the connection and outstanding subscriptions.

§Examples

For example code, please refer to the examples directory.

Implementations§

source§

impl ConvexClient

source

pub async fn new(deployment_url: &str) -> Result<Self>

Constructs a new client for communicating with deployment_url.

let client = ConvexClient::new("https://cool-music-123.convex.cloud").await?;
source

pub async fn subscribe( &mut self, name: &str, args: BTreeMap<String, Value> ) -> Result<QuerySubscription>

Subscribe to the results of query name called with args.

Returns a QuerySubscription which implements [Stream]< FunctionResult>. A new value appears on the stream each time the query function produces a new result.

The subscription is automatically unsubscribed when it is dropped.

let mut client = ConvexClient::new("https://cool-music-123.convex.cloud").await?;
let mut sub = client.subscribe("listMessages", maplit::btreemap!{}).await?;
while let Some(result) = sub.next().await {
    println!("{result:?}");
}
source

pub async fn query( &mut self, name: &str, args: BTreeMap<String, Value> ) -> Result<FunctionResult>

Make a oneshot request to a query name with args.

Returns a FunctionResult representing the result of the query.

This method is syntactic sugar for waiting for a single result on a subscription. It is equivalent to client.subscribe(name, args).await?.next().unwrap()

let mut client = ConvexClient::new("https://cool-music-123.convex.cloud").await?;
let result = client.query("listMessages", maplit::btreemap!{}).await?;
println!("{result:?}");
source

pub async fn mutation( &mut self, name: &str, args: BTreeMap<String, Value> ) -> Result<FunctionResult>

Perform a mutation name with args and return a future containing the return value of the mutation once it completes.

let mut client = ConvexClient::new("https://cool-music-123.convex.cloud").await?;
let result = client.mutation("sendMessage", maplit::btreemap!{
    "body".into() => "Let it be.".into(),
    "author".into() => "The Beatles".into(),
}).await?;
println!("{result:?}");
source

pub async fn action( &mut self, name: &str, args: BTreeMap<String, Value> ) -> Result<FunctionResult>

Perform an action name with args and return a future containing the return value of the action once it completes.

let mut client = ConvexClient::new("https://cool-music-123.convex.cloud").await?;
let result = client.action("sendGif", maplit::btreemap!{
    "body".into() => "Tatooine Sunrise.".into(),
    "author".into() => "Luke Skywalker".into(),
}).await?;
println!("{result:?}");
source

pub fn watch_all(&self) -> QuerySetSubscription

Get a consistent view of the results of multiple queries (query set).

Returns a QuerySetSubscription which implements [Stream]<QueryResults>. Each item in the stream contains a consistent view of the results of all the queries in the query set.

Queries can be added to the query set via ConvexClient::subscribe. Queries can be removed from the query set via dropping the QuerySubscription token returned by ConvexClient::subscribe.

QueryResults is a copy-on-write mapping from SubscriberId to its latest result Value.

let mut client = ConvexClient::new("https://cool-music-123.convex.cloud").await?;
let mut watch = client.watch_all();
let sub1 = client.subscribe("listMessages", maplit::btreemap!{
    "channel".into() => 1.into(),
}).await?;
let sub2 = client.subscribe("listMessages", maplit::btreemap!{
    "channel".into() => 1.into(),
}).await?;
source

pub async fn set_auth(&mut self, token: Option<String>)

Set auth for use when calling Convex functions.

Set it with a token that you get from your auth provider via their login flow. If None is passed as the token, then auth is unset (logging out).

Trait Implementations§

source§

impl Clone for ConvexClient

Clone the ConvexClient, sharing the connection and outstanding subscriptions.

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Drop for ConvexClient

Drop the ConvexClient. When the final reference to the ConvexClient is dropped, the connection is cleaned up.

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more