0

I'm writing a chat powered by twilio API. I have a problem with creating a channel and then obtaining all of its members.

The flow is following:

  • Frontend hits an endpoint on the backend which is responsible for creating a channel
  • Backend creates a channel
  • On the Frontend I receive event channelAdded (this is from twilio sdk)
  • In the event callback I receive a channel as an argument
  • I call .getMembers() method on this newly created channel

And... the problem is that this method returns wrong number of members. I created a channel with 5 members, but the method returned only 3 of them. After I refresh the page everything works as expected - .getMembers() returns 5 members. It looks like there might be some race conditions somewhere. I could "wait" on every new channel a second or two - it feels very hacky, but probably solves this issue.

Am I doing something wrong?

feerlay
  • 2,286
  • 5
  • 23
  • 47

1 Answers1

1

Twilio developer evangelist here.

You're likely right in that this is a race condition. I would approach this by adding a memberJoined listener to the channel as soon as you receive the channelAdded event. That way you can load the existing members with .getMembers() and catch any still being added with the event listener.

Let me know if that helps at all.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Just a bit off topic but what ought one to do if author member gets 54007 on Channel.getMembers()? When I add the members, I run Channel.add(identity) for both the author and other user. – KasparTr May 11 '20 at 10:37
  • Hey @KasparTr, the comments aren’t a great place for off topic questions. There’s not enough space to get the details. I’d recommend asking a new question with as much detail as possible. – philnash May 11 '20 at 11:16
  • you are totally right. Added new question here https://stackoverflow.com/questions/61728239/twilio-chat-channel-getmembers-method-access-forbitten – KasparTr May 11 '20 at 11:48