pub struct BaseConvexClient { /* private fields */ }
Expand description

The synchronous state machine for the ConvexClient. It’s recommended to use the higher level ConvexClient unless you are building a framework.

This struct should be used instead of the ConvexClient when you want the ability to build consistent client views. For example, in order to use your own websocket manager or make a client compatible with another language (e.g. Swift or Python).

For the latter use case, we strongly recommend you to take a look at the implementation of the ConvexClient. The recommended pattern to use an BaseConvexClient is to create a background thread to manage actions on queries/mutations and incoming websocket connections, and use that to advance the BaseConvexClient’s state.

§Managing Convex State

The main methods, subscribe, unsubscribe, and mutation directly correspond to its equivalent for the external ConvexClient.

The only different method is get_query, which returns the current value for a query given its query id. This method can be used to synchronously request the current value, as opposed to a stream of values in subscribe.

Note: these methods have the side effect of adding messages to be sent to the server, so you would need to flush all outgoing messages by looping on pop_next_message after each call of the above functions.

§Watching for consistent updates to queries

To watch for consistent changes in query values, you can add the following code to the background thread:

use convex::base_client::BaseConvexClient;
use convex::Value;
use convex_sync_types::ServerMessage;

fn on_receive_server_message(mut base_client: BaseConvexClient, msg: ServerMessage<Value>) {
    let res = base_client.receive_message(msg).expect("Base client error");
    if let Some(latest_result_map) = res {
        for (subscriber_id, function_result) in latest_result_map.iter() {
            // Notify components of the updated_value
        }
    }
}

§Managing Web Socket States

To manage websocket messages, use receive_message (for incoming messages from the server) and pop_next_message (for outgoing messages to send to the server). The BaseConvexClient does not send these messages, so you will have to regularly monitor if there are messages to be sent by calling pop_next_message.

Additionally, when the websocket reconnects, you should call resend_ongoing_queries_mutations and loop on pop_next_message to resend requests to the Server to resubscribe to queries and perform ongoing mutations.

§pop_next_message should be called after the following methods:

Implementations§

source§

impl BaseConvexClient

source

pub fn new() -> Self

Construct a new BaseConvexClient.

source

pub fn subscribe( &mut self, udf_path: UdfPath, args: BTreeMap<String, Value> ) -> SubscriberId

Update state to be subscribed to a query and add subscription request to the outgoing message queue.

After calling this, it is highly recommended to loop on pop_next_message to flush websocket messages to the server.

source

pub fn unsubscribe(&mut self, subscriber_id: SubscriberId)

Update state to be unsubscribed to a query and add unsubscription request to the outgoing message queue.

After calling this, it is highly recommended to loop on pop_next_message to flush websocket messages to the server.

source

pub fn get_query(&self, query_id: QueryId) -> Option<FunctionResult>

Return the local value of a query.

source

pub fn mutation( &mut self, udf_path: UdfPath, args: BTreeMap<String, Value> ) -> Receiver<FunctionResult>

Track mutation and add mutation request to the outgoing message queue.

After calling this, it is highly recommended to loop on pop_next_message to flush websocket messages to the server.

source

pub fn action( &mut self, udf_path: UdfPath, args: BTreeMap<String, Value> ) -> Receiver<FunctionResult>

Track action and add action request to the outgoing message queue.

After calling this, it is highly recommended to loop on pop_next_message to flush websocket messages to the server.

source

pub fn set_auth(&mut self, token: AuthenticationToken)

Set auth on the sync protocol.

source

pub fn pop_next_message(&mut self) -> Option<ClientMessage>

Pop the next message from the outgoing message queue.

Note that this does not send the message because the Internal client has no awareness of websockets. After popping the next message, it is the caller’s responsibility to actually send it.

source

pub fn max_observed_timestamp(&self) -> Option<Timestamp>

Returns the maximum timestamp observed by the client.

source

pub fn receive_message( &mut self, message: ServerMessage<Value> ) -> Result<Option<QueryResults>, String>

Given a message from a Server, update the base state accordingly.

source

pub fn latest_results(&self) -> &QueryResults

Grab a snapshot of the latest query results to all subscribed queries.

source

pub fn resend_ongoing_queries_mutations(&mut self)

Resend all subscribed queries and ongoing mutations. Should be used once the websocket closes and reconnects.

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, 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