1

After creating a channel in the Twilio Programmable Chat service, I am listening to the client events.

One of those events is the channelAdded event. In the hook, I have to retrieve the Members of the channel (in order to get the opposite members name in a binary room) like so

channel.getMembers()

When I do this, Twilio returns:

name: "SyncError"
message: "Access forbidden for identity (status: 403, code: 54007)"
status: 403
code: 54007

It would make sense if the user that is accessing that method wouldn't also be the author and a member of that channel.

Do I need to grant the author some special rights to access the channel?

Additional, secondary question

When I create a binary channel (2 members), I need to manually add the author as the member of the channel also like so:

        var channel = await this.client.createChannel({
            friendlyName: command.roomName,
            isPrivate: command.isPrivateRoom
        })
        var p1 = channel.add(command.currentUserId);
        var p2 = channel.add(command.oppositeUserId);
        await Promise.all([p1,p2])
        return command.roomName;

Is there a shortcut or a way to auto, add the Member on creation?

Skaranjit
  • 764
  • 9
  • 26
KasparTr
  • 2,328
  • 5
  • 26
  • 55

1 Answers1

2

Twilio developer evangelist here.

As per the documentation:

Once you've created a channel, a user must join it to begin receiving or sending messages on that channel.

Whether you create the channel in the client or server side, you need to specifically join the channel with your user before they have access to it. The only shortcut here is that you can call join() on the channel to join it with the authenticated user. For example:

await channel.join();
const members = await channel.getMembers();
philnash
  • 70,667
  • 10
  • 60
  • 88
  • That's great, thanks! Will the member have to join it once in the lifecycle of a channel or more often? Since i'm adding all members programmatically (think fb messenger) should they be joining in the channelAdded() listener? Is there a higher view on all the lifecycle requirements somewhere? – KasparTr May 12 '20 at 05:26
  • Once you join a channel you are a member of that channel and you will appear in that channel's members list until you leave. You don't have to join it over and over. – philnash May 12 '20 at 05:35
  • When creating a channel, I need to add another member into it without invitation. For that I will use channel.add(). Now the other identity is also a Member. The problem is that while current identiy can join a channel right after creating it, I cannot do that for the other user (join has no args). i.e the other itentity has no way to know when to join a channel but in the channelAdded event?! In which case the join needs to be called each time the event is triggered? – KasparTr May 12 '20 at 05:51
  • The `channelAdded` event is fired for all ["created and not joined private channels" and "all type of channels Client has joined or invited to"](http://media.twiliocdn.com/sdk/js/chat/releases/3.3.5/docs/Client.html#event:channelAdded__anchor). You can also check the user's status in the channel with the [Channel#Status() function](http://media.twiliocdn.com/sdk/js/chat/releases/3.3.5/docs/Channel.html#Status) which returns "unknown", "known", "invited" or "joined". – philnash May 12 '20 at 05:58
  • I read that but not sure how that will help to understand when they triggered or how to join another Identity to a channel programmatically. As far as I understand, when `channel.add( – KasparTr May 12 '20 at 06:38
  • If you `channel.add(user)` then that is joining the channel on their behalf so you don't need them to join the channel. I would check that `channel.status` property (sorry, it's a property, not a function) to see if they need to join the channel. Also, my read on the `channelAdded` event is that it will only produce a channel that they haven't already joined if it is a private channel that they created but haven't joined yet. So, your remote user in this situation should receive the `channelAdded` event and already have joined it, so can then perform functions on the channel object. – philnash May 12 '20 at 07:09
  • Hi guys, If the client needs to do join after creating a channel why the Twilio admin tool shows 1 member on the channels page? there should be no members yet. – jjalonso Jun 11 '20 at 01:24
  • 1
    @jjalonso I don't know I'm afraid! You might need to take that up with [Twilio support](https://www.twilio.com/help/contact). – philnash Jun 11 '20 at 05:35
  • Im facing a similar issue and maybe you can be a great help, I need to execute getMembers() on channelAdded event, but it fail if I don't run join() first, but of course running join() will give me 'maximum members per channel reached' because i could be already IN, the funny thing is that if I check channel.status before, it always give me 'known', included the first time before joining. all my example is from my user, the channel creator. – jjalonso Jun 11 '20 at 14:55
  • @jjalonso the comments of another question aren't the best place to discuss this. Can you perhaps ask a new question and fill in as many details as you can. Also, "known" is only a status of the channel relative to the client itself, the other statuses are "unknown", "joined" or "invited" (http://media.twiliocdn.com/sdk/js/chat/releases/3.4.0/docs/Channel.html#Status). – philnash Jun 12 '20 at 02:05