Channel Initialization

Last Edit: Jun 06 2020

Here’s how you can initialize a single channel for later use. Please note that this will only create a channel object without making any API call. Once the channel is initialized you can use one of these methods: query, watch, create.


const conversation = client.channel('messaging', 'channel-name', {
    name: 'Founder Chat',
    image: 'http://bit.ly/2O35mws',
    members: ['thierry', 'tommaso'],
});

await conversation.create();
                    

conversation = client.channel(
    'messaging', 'thierry-tommaso-1', 
    data: {
      'name'   => 'Founder Chat',
      'image'  => 'http://bit.ly/2O35mws',
      'members => ['thierry', 'tommaso'],
    }
)
                    

final channel = client.channel(
  "messaging",
  id: "travel",
  extraData: {
    "name": "Founder Chat",
    "image": "http://bit.ly/2O35mws",
    "members": ["thierry", "tommaso"],
  },
);
                    

$channelConf = [ 
	'name' => 'Founder Chat',
	'image' => 'http://bit.ly/2O35mws',
  'members' => ['thierry', 'tommaso']
];

$channel = $client->Channel("messaging", "thierry-tommaso-1", $channelConf);
                    

val channelController = client.channel(channelType, channelId)

val extraData = mutableMapOf<String, Any>()
val members: MutableList<String> = ArrayList()

extraData["name"] = "Founder Chat"
extraData["image"] = "http://bit.ly/2O35mws"

members.add("thierry")
members.add("tommaso")

channelController
    .create(members, extraData)
    .enqueue {  result ->
        if (result.isSuccess) {
            val channel = result.data()
        } else {
            val error = result.error()
        }
    }
                    

// Create an extra data for a channel.
// ChannelExtraDataCodable is an extension of Codable that makes sure you have common fields 
// used by the UI library (name and imageURL)
struct MyChannelData: ChannelExtraDataCodable {
    var name: String?
    var imageURL: URL?
    let info: String // this is our custom field :)
}

// Register once your channel extra data type, we'll need it for decoding
Channel.extraDataType = MyChannelData.self

let data = MyChannelData(name: "Travel", imageURL: nil, info: "Awesome channel about traveling")
let channel = Client.shared.channel(type: .messaging, id: "travel", extraData: data)

// Create the channel
channel.create { (result) in
    // handle result
}
                    

ChannelController channelController = client.channel(channelType, channelId);

Map<String, Object> extraData = new HashMap<>();
List<String> members = new ArrayList<>();

extraData.put("name", "Founder Chat");
extraData.put("image", "http://bit.ly/2O35mws");

members.add("thierry");
members.add("tommaso");

channelController.create(members, extraData).enqueue(result -> {
    if (result.isSuccess()) {
        Channel channel = result.data();
    } else {
        ChatError error = result.error();
    }
    return Unit.INSTANCE;
});
                    

Channel Initialization Parameters

Name Type Description Default Optional
type string The channel type. Default types are livestream, messaging, team, gaming and commerce. You can also create your own types. -
id string The channel id (optional). If you don't specify an ID the ID will be generated based on the list of members. (max length 64 characters) -
channel data string Extra data for the channel. Must not exceed 5KB in size. default
The channel data can contain any number of custom fields. However, there are a few reserved fields that Stream uses:

Reserved Fields for Custom Channel Data

Name Type Description
name string The channel name. No special meaning, but by default the UI component will try to render this if the property is present.
image string The Channel mage. Again there is no special meaning but by default, the UI component will try to render this is the property is present.
members array The members participating in this Channel. Note that you don't need to specify members for a live stream or other public chat. You only need to specify the members if you want to limit access of this chat to these members and subscribe them to future updates