In a Programmable Chat application, a Channel is where all the action happens. Whether a chat is between two users or two hundred, a Channel is where messages are sent, received, and archived for later viewing by offline clients.
Programmable Chat has two objects for working with channels -
Channel Descriptors and
Channels. A Channel Descriptor is a lightweight representation of the key attributes of a public or private channel. When requesting a list of channels, you will receive Channel Descriptors that serve as a snapshot in time for each channel. These channel attributes will not update if modified remotely, so you should use the descriptors only to assist your user in discovering and joining a channel.
- Channel SID
- Created By
- Channel Attributes
- Messages and Members Count
A full Channel object allows you to join and interact with the channel. Let's dive into a few of the key techniques you'll need to employ while working with channels and messages in your application.
If your programmable chat instance has public channels that are discoverable by a user, you can fetch a list of Channel Descriptors for all public channels in the system:
client.getChannel() gets the channel descriptor and instantiates the channel internally for you.
Before you can start sending messages, you first need a Channel that can receive messages. Here is how you create a channel.
Once you've created a channel, a user must join it to begin receiving or sending messages on that channel.
message is a bit of data that is sent first to the Twilio backend where it is stored for later access by members of the channel. The message is then pushed out in real time to all channel members currently online. Only users subscribed to your channel will receive your messages.
Today, a message is just a string of text. Available in beta you can also send other media types, like images and binary data.
手元にあるチャネルオブジェクトを使用すると、チャネルから最新のメッセージを取得できます。 これを使用してチャンネル内の履歴を提供します。 取得したいメッセージ数を選択できます。
You can also be notified of any new incoming messages with an event handler. Use this handler to update your user interface to display new messages.
Sometimes you might feel lonely in a channel. Rather than sending messages to yourself, invite a friend to come and chat! It doesn't matter if the channel is public or private - you are always able to invite another user to any channel you own.
Social acceptance is a great feeling. Accepting an invite to a channel means you too can partake in banter with other channel members.
Listing subscribed channels lets you perform actions on them as a channel member (e.g.,
display). This method only shows the channels where the current programmable chat user is a member. To list public channels that have not yet been joined, see list public channels above.
Channels are a flurry of activity. Members join and leave, messages are sent and received, and channel states change. As a member of a channel, you'll want to know the status of the channel. You may want to receive a notification when the channel is deleted or changed. Channel event listeners help you do just that.
These event listeners will notify your app when a channel's state changes. Once you receive the notification, you can perform the necessary actions in your app to react to it.
Your event listeners will also notify your app when channel members perform some action (including when they leave, join, change, or start/stop typing). Using these listeners, you can provide real-time updates to your application users.
You can only delete channels that you have permissions to delete. Deleting a channel means it cannot be retrieved at a later date for any reason. Delete with caution!
Now that you know all there is to know about channels, might we suggest learning more about the REST API? With the REST API, you can execute many of these same actions from your server-side code.
Next: Media Support in Chat