Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Websocket::with_message_stream for client message customization #501

Merged

Conversation

D1plo1d
Copy link
Contributor

@D1plo1d D1plo1d commented May 7, 2021

This PR adds a Websocket::with_message_stream() in order to allow developers to customize the ClientMessages received by their Websocket.

Specifically, Websocket::with_message_stream() can be used to implement file uploads over websockets as well as websocket serialization alternatives such as msgpack.*

* Note: At the moment a developer implementing a msgpack websockets would need to convert their serialization errors to serde_json::error. This is less ergonomic then ideal but it is hoped that it is workable as a first API iteration.

Primary Motivation

Enable developers to explore uploading files over non-HTTP channels.

Secondary Motiviation

Allows other ClientMessage customizations.

API Changes

  • Added Websocket::with_message_stream() to allow developers to customize the ClientMessage creation for their websocket.
  • Exposed ClientMessage as pub (for Websocket::with_message_stream)
  • Added ClientMessage::from_bytes() to keep code duplication at a minimum

Example Usage

struct FutureProtocol {
    msg: Vec<u8>,
    uploads: UploadValue,
}

let message_stream = message_stream.map(|msg: FutureProtocol| {
    let mut client_message = ClientMessage::from_bytes(msg.payload)?;

    if let ClientMessage::Start {
        payload: request,
        ..
    } = &mut client_message {
        request.uploads = msg.uploads;
    }

    Ok(client_message)
});

let connection = async_graphql::http::WebSocket::with_message_stream(
    schema,
    message_stream,
    initializer,
    async_graphql::http::WebSocketProtocols::GraphQLWS,
)

Example usage in an application: https://github.com/tegapp/teg/blob/develop/crates/server/src/main.rs

@sunli829
Copy link
Collaborator

sunli829 commented May 8, 2021

Thank you, this PR looks great! 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants