When using certain Stream update endpoints, it is important to note that custom key-value data may be lost, if it is not included in the most recent update. The below endpoints behave in this way and this article will show you how to handle these endpoints properly to keep your data safe. You can also use Stream partial update endpoints to avoid any data loss during updates. If you wish to use a partial update to sidestep this risk, you can find partial channel updates and partial user updates in our docs pages.
1. Update Channel - channel.update()
When updating channels, it is important to include all current custom key-value data in the update.
Below is an example of how Update Channel can wipe key-value data during updates. The first update provides data for name, color, and location for the channel.
const update = await channel.update(
{
name: 'Example Chat',
color: 'yellow',
location: 'CO'
},
);
Now if we run another update again, but without the "location" data field, it is removed from the channel.
const update = await channel.update(
{
name: 'Example Chat',
color: 'yellow',
},
);
2. Update User - client.updateUser()
The Update User endpoint is similar to the Update Channel endpoint in that it will wipe all custom key-value pair data not included in the most recent update. It also has a partial update alternative.
Just like with Update Channel, if we update the user's favorite color to be purple
const update= {
id: userID,
set: {
role: "admin",
happy: 'yes,'
favorite_color: 'purple',
},
};
const new_update = await client.updateUser(update);
And then leave out their favorite color from our next update
const update= {
id: userID,
set: {
role: "admin",
happy: 'yes,'
},
};
const new_update = await client.updateUser(update);
The user will no longer have a key-value data field for their favorite color.
Note: When you call the Send Message endpoint using server-side auth and set the user in the message object, you will also trigger a user update, so be mindful of not losing any data as shown above if you choose to do so.
3. Update Message - client.updateMessage()
The update message endpoint can present a risk of losing message text data. An update to the message text will replace the previous text and remove it from the message object.
In this example, we will first run the following code to send a message:
const message = await channel.sendMessage(
{
text: "Original message!",
},
)
Next, we will run an update on the message:
const newMessage = {
id: 'aboveMessageID',
text: '',
user_id: 'user1'
};
The original message is now be overwritten and isn't saved in the message object. The message text is an empty string.
Review
The above endpoints Update Channel and Update User will not save key-value custom data not provided in the most recent update. You can always use partial update endpoints to avoid the risks around data retention discussed above. Update Message will also not save message text not included in a message update. Ensure you keep this in mind while building your Stream Chat Application. To learn more, please take a look at the related resources linked in this article and reach out to https://getstream.io/contact/support/ if you have any questions!
Related Resources:
Update Channel Docs - https://getstream.io/chat/docs/channel_update/?language=js
Update Users Docs - https://getstream.io/chat/docs/update_users/?language=js&q=update+user
Message Docs - https://getstream.io/chat/docs/send_message/?language=js
Comments
0 comments
Please sign in to leave a comment.