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

feat: support prepareMessage and sendPreparedMessage #100

Merged
merged 1 commit into from
Sep 7, 2023

Conversation

dmccartney
Copy link
Contributor

This adds support for .preparedMessage() to the SDK with a specific eye toward supporting robust pending-message queues.

This fixes #82
See also xmtp/xmtp-ios#152 and xmtp/xmtp-android#113

@dmccartney dmccartney requested a review from a team as a code owner September 7, 2023 04:05
Comment on lines +105 to +109
const prepared = await bobConversation.prepareMessage("hi");

// Either of these should work:
await bobConversation.sendPreparedMessage(prepared);
// await bob.sendPreparedMessage(prepared);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the expected typical scenario for JS-only apps -- calling .prepareMessage immediately upon user input and then attempting .sendPreparedMessage (or waiting to (re)try when connectivity resumes).

Comment on lines +52 to +69
// For native integrations (e.g. if you have native code for a robust
// pending-message queue in a background task) you can load the referenced
// `preparedFileUri` as a serialized `PreparedMessage` with the native SDKs.
// The contained `envelopes` can then be directly `.publish()`ed with the native `Client`.
// e.g. on iOS:
// let preparedFileUrl = URL(string: preparedFileUri)
// let preparedData = try Data(contentsOf: preparedFileUrl)
// let prepared = try PreparedMessage.fromSerializedData(preparedData)
// try await client.publish(envelopes: prepared.envelopes)
// e.g. on Android:
// val preparedFileUri = Uri.parse(preparedFileUri)
// val preparedData = contentResolver.openInputStream(preparedFileUrl)!!
// .use { it.buffered().readBytes() }
// val prepared = PreparedMessage.fromSerializedData(preparedData)
// client.publish(envelopes = prepared.envelopes)
//
// You can also stuff the `preparedData` elsewhere (e.g. in a database) if that
// is more convenient for your use case.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some apps implement a native background task for robust pending-message queues (e.g. to gracefully handle outages and restarts outside of a JS context). Here are some pieces of a recipe to handle publishing these prepared messages using the native SDKs.

Copy link
Contributor

@nplasterer nplasterer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all the extra context added in here! Looks good.

@dmccartney dmccartney merged commit 1ba55a6 into main Sep 7, 2023
3 of 4 checks passed
@dmccartney dmccartney deleted the daniel-prepared-messages branch September 7, 2023 04:46
@github-actions
Copy link

github-actions bot commented Sep 7, 2023

🎉 This PR is included in version 1.12.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

This was referenced Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Prepare Messages
2 participants