xmtp / xmtp-js

XMTP client SDKs, content types, and other packages written in TypeScript
https://xmtp.org/docs
211 stars 40 forks source link

Feature request: Message serialization #291

Closed neekolas closed 10 months ago

neekolas commented 1 year ago

Is your feature request related to a problem?

When retrieving messages from the SDK it is difficult to serialize them.

There are two problems that make serialization of DecodedMessage objects difficult:

  1. Message contents are of an any type, and may not be serializable. XMTP does not put any constraints onto the output of a codec. User defined codecs may return classes, functions, or any other data type.
  2. Each message includes a reference to the Conversation it originated in for convenience

Describe the solution to the problem

We should create a message.toBytes() method that serializes to a Uint8Array and a DecodedMessage.fromBytes(data) static method for deserialization.

To accomplish this we need to make two changes to the SDK.

  1. Pass the decrypted message content as bytes into the DecodedMessage class, so that the original bytes can be stored in the serialized version and decodeContent can be re-run against the raw bytes as part of deserialization.
  2. Make Conversations serializable as ConversationReference objects

The good news is that 2 will be coming as part of v8 of the JS SDK as part of the migration to the Keystore. 1 is a straightforward change.

Describe the uses cases for the feature

Additional details

/cc @tg44

tg44 commented 1 year ago

Yes, exactly! We had no problems with the content, because we did not see any content types that we can't serialise to json and read back. (We used text, json and base64 encoded stuff.) But you are right, nothing permits the users to create a content type that is not serialisable.

rygine commented 10 months ago

this has been implemented