Releases: xmtp/xmtp-react-native
v1.22.0
v1.21.0
1.21.0 (2023-12-02)
This release introduces support for custom content types and a breaking change to message content.
Custom content types
React Native now supports custom codecs, enabling you to send any type of content that makes sense for you and your app.
Important: Your custom content type WILL NOT automatically be supported by other apps. Other apps will display the fallback text defined in your custom codec instead.
First, create a custom codec and be sure to include fallback text:
const ContentTypeNumber: ContentTypeId = {
authorityId: 'xmtp.org',
typeId: 'number',
versionMajor: 1,
versionMinor: 0,
}
class NumberCodec implements JSContentCodec<number> {
contentType = ContentTypeNumber
// a completely absurd way of encoding number values
encode(content: number): EncodedContent {
return {
type: ContentTypeNumber,
parameters: {
number: JSON.stringify(content),
},
content: new Uint8Array(),
}
}
decode(encodedContent: EncodedContent): number {
return JSON.parse(encodedContent.parameters.number) as number
}
fallback(content: number): string | undefined {
return 'A number was sent.'
}
}
Then, when spinning up a client, instantiate the custom codec and register it.
const client = await Client.create({
env: 'production',
codecs: [new NumberCodec()],
})
Now you can encode, send, and decode messages using your custom content type by passing in an optional SendOptions
.
await conversation.send(12, { contentType: ContentTypeNumber })
Breaking change
XMTP used to register supported codecs for you by default. Now you must register all codecs you’d like to support manually on client create.
const client = await Client.create(wallet, {
env: 'production',
codecs: [new XMTP.ReactionCodec(), new XMTP.ReplyCodec(), new XMTP.ReadReceiptCodec(), new XMTP.RemoteAttachmentCodec(), new XMTP.StaticAttachmentCodec()],
})
Because of this message content is now a function which returns the actual content. You can get that content by call message.content()
now instead of message.content
. This may involve more filtering on the message side to make sure you are handling different contentTypes appropriately.
For a list of defined content types, see Native Codecs.
Features
- fix up small code issues (6798b64)
What's Changed
- In line code comment documentation by @kele-leanes in #152
- Android side of custom content types by @nplasterer in #156
- Custom content types by @nakajima in #155
- Update README.md by @WilliamYWY in #157
- add staticCanMessage method by @kele-leanes in #158
New Contributors
- @kele-leanes made their first contribution in #152
- @WilliamYWY made their first contribution in #157
Full Changelog: v1.20.2...v1.21.0