0

How do i create a user and join that user into a channel with a friendly name ?

All the examples in the documentation located here assumes that a user with appropriate friendlyName is already present in the service instance.

So, if i want to join a user in my system to a channel, Do i need to first create a user using users rest api endpoint and then add that user as a member of a channel ?

channel.members.create(user.identity)

Above code above throws 409 conflict error if i re-create a user. So i am forced to fetch a user, see if the user exists then create the user. If user already exists, then I have to update the user with friendlyName.

I am forced to check if the user in my system exists in that service instance everytime that user joins a channel.

Is there a way I can join a user to a channel with friendly name so Twilio creates if the user doesn't exist and update friendly name if user already is in twilio service instance ?

This is the sequence of events I would like to see:

  1. User in my system clicks join room button
  2. Either from client side or server side, I join the user into that channel with identity and friendlyName.
  3. Twilio creates a user/member in service instance and channel if the user/member doesnt exist Twilio updates a user/members friendly name if the user already exists.
Koder
  • 1,794
  • 3
  • 22
  • 41
  • I'm a developer evangelist for Twilio and I'd like to help. I'm a little confused though, are you trying to do this through the SDK (JS? Swift? Android?) or by the REST API? And which would be the ideal way to do it? – philnash Oct 03 '17 at 10:03
  • Ideal would be through REST API. since i don't want the client side to have the ability to create users. – Koder Oct 03 '17 at 10:07
  • You don't really create a user on the client side though. You create an access token on your server, pass that to the client side and when you connect using that token for the first time you create a user at that point. Then, when that user joins channels in the client side you shouldn't receive errors. – philnash Oct 03 '17 at 10:14
  • Yes, but i couldn't update friendlyName for that user. I had to do that from server. Since in my application friendlyName can change from time to time, I had to make sure friendlyName in twilio also remained in sync. Anyway i decided to not make this a problem at all. I chose to just use the identity and use my own webservice to fetch friendlyName. But a recommendation I have is to have access token creation service to include both identity and friendlyname like this: accessToken.user = { identity:u_key, friendlyName: "bob baker" } instead of accessToken.identity = u_key. – Koder Oct 03 '17 at 11:36
  • I can see why you would want to set the initial friendly name in the token, though all users when they are authenticated with the client side SDK can update their own info with `client.user.updateAttributes({ friendlyName: 'Phil' })` which you can run once the client connects for the first time. Then, in the backend when a user updates their name you should be able to use the identity to update via the REST API. Throughout the REST API, you should use the `identity` as that is unique, unlike `friendlyName`. – philnash Oct 03 '17 at 12:25
  • "which you can run once the client connects for the first time". How will I know if it's a users first time ? I need to keep track of that part too. I ofcourse use Identity everywhere, but since your documentation said that Twilio creates a user if one is not created before the first time it encounters new user, I thought may be it can update user details too. Since it would save some plumbing on my side. – Koder Oct 03 '17 at 13:05
  • You could store your user's Programmable Chat SID against their database record in your system? That way you'd also be able to refer to them within chat from a different context. Or just set their friendlyName to the latest each time they connect. The documentation is referring to users with the SDKs. The REST API tries to behave like a regular REST API which would return a 409 in the situation you described before. – philnash Oct 03 '17 at 13:15

1 Answers1

0

2022 answer:

const participant = await conversation.getParticipantByIdentity(userIdentity);
if (!participant) {
 await conversation.join();
}

And yes, yon need to use Conversation API.

devyatov
  • 1
  • 1