Updating a Channel

There are two ways to update a channel using the Stream API - a partial or full update. A partial update will retain any custom key-value data, whereas a complete update is going to remove any that are unspecified in the API request.

Partial Update

A partial update can be used to set and unset specific fields when it is necessary to retain additional custom data fields on the object. AKA a patch style update.

// Android SDK

// Here's a channel with some custom field data that might be useful
ChannelClient channelClient = client.channel("messaging", "general");

List<String> members = Arrays.asList("thierry", "tommaso");

Map<String, String> channelDetail = new HashMap<>();
channelDetail.put("topic", "Plants and Animals");
channelDetail.put("rating", "pg");

Map<String, Integer> userId = new HashMap<>();
userId.put("user_id", 123);

Map<String, Object> extraData = new HashMap<>();
extraData.put("source", "user");
extraData.put("source_detail", userId);
extraData.put("channel_detail", channelDetail);

channelClient.create(members, extraData).execute();

// let's change the source of this channel
Map<String, Object> setField = Collections.singletonMap("source", "system");
channelClient.updatePartial(setField, emptyList()).execute();

// since it's system generated we no longer need source_detail
List<String> unsetField = Collections.singletonList("source_detail");
channelClient.updatePartial(emptyMap(), unsetField).execute();

// and finally update one of the nested fields in the channel_detail
Map<String, Object> setNestedField = Collections.singletonMap("channel_detail.topic", "Nature");
channelClient.updatePartial(setNestedField, emptyList()).execute();

// and maybe we decide we no longer need a rating
List<String> unsetNestedField = Collections.singletonList("channel_detail.rating");
channelClient.updatePartial(emptyMap(), unsetNestedField).execute();

// Backend SDK
Channel.partialUpdate("messaging", "general")
  .setValue("source", "system")
  .setValue("channel_detail.topic", "Nature")
  .user(user)
  .unsetValue("source_detail")
  .request()
  .getChannel();

Full Update (overwrite)

The update function updates all of the channel data. Any data that is present on the channel and not included in a full update will be deleted.

// Android SDK
ChannelClient channelClient = client.channel("messaging", "general");

Map<String, Object> channelData = new HashMap<>();
channelData.put("name", "myspecialchannel");
channelData.put("color", "green");
Message updateMessage = new Message();
updateMessage.setText("Thierry changed the channel color to green");

channelClient.update(updateMessage, channelData).enqueue(result -> {
  if (result.isSuccess()) {
    Channel channel = result.data();
  } else {
    // Handle result.error()
  }
 });


// Backend SDK
MessageRequestObject msg = MessageRequestObject
  .builder()
    .text("Thierry changed the channel color to green")
  .build();

Channel.update("messaging", "general")
  .message(msg)
  .data(ChannelRequestObject.builder()
   .additionalField("name", "myspecialchannel")
   .additionalField("color", "green")
   .build())
  .request();

Request Params

NameTypeDescriptionOptional
channel dataobjectObject with the new channel information. One special field is “frozen”. Setting this field to true will freeze the channel. Read more about freezing channels in “Freezing Channels”
textobjectMessage object allowing you to show a system message in the Channel that something changed.Yes

Updating a channel using these methods cannot be used to add or remove members. For this, you must use specific methods for adding/removing members, more information can be found here.

Archiving a channel

A member in a channel can mark the channel as archived. The archival state is stored for this member, so it doesn’t affect anybody else. From an API point of view, an archived channel is the same as another channel, but the user interface may render an archived channel in a different way. When a channel is archived, the current date is recorded and this is returned as archived_at in the response.

When querying channels, the query can specify specify that archived channels should be excluded. The query can also specify only archived channels.

// Archive the channel for user amy.
Channel.archive(channel.getType(), channel.getId(), "amy").request();

// Query for amy's channels that are archived.
Channel.list()
    .userId("amy")
    .filterCondition(FilterCondition.in("members", "amy"))
    .filterCondition("archived", true)
    .request();

// Unarchive
Channel.unarchive(channel.getType(), channel.getId(), "amy").request();

Pinning a channel

Pinning works very similar to archiving. A client can mark a channel as pinned, which can be used to render it differently in the UI. When a channel is pinned, the response contains a pinned_at timestamp.

Like archiving, it’s possible to query channels with a specific pinned state. It is also possible to sort channels by pin, such as returning pinned channels first.

// Pin the channel for user amy.
Channel.pin(channel.getType(), channel.getId(), "amy").request()

// Query for amy's channels that are pinned.
Channel.list()
    .userId("amy")
    .filterCondition(FilterCondition.in("members", "amy"))
    .filterCondition("pinned", true)
    .request();

// Query for channels for specific members and show pinned first.
Channel.list()
    .userId("amy")
    .filterConditions(FilterCondition.in("members", "amy", "ali"))
    .sort(Sort.builder().field("pinned_at").direction(Direction.DESC).build())
    .request();

// Unpin
Channel.unpin(channel.getType(), channel.getId(), "amy").request()

Global archiving or pinning

Channels are archived or pinned for a specific member. If the channel should instead be archived or pinned for all users, the this can be stored as custom data in the channel itself. The value cannot collide with the existing fields, so use a value such as globally_archived: true.

© Getstream.io, Inc. All Rights Reserved.