Manage the user-channel membership relationship

Requires App Context

Enable App Context for your keyset in the Admin Portal.

A Membership entity is created when a user joins or is invited to a channel, and ends when the user leaves.

Get membership​

Get all channel memberships for a user or channel with GetMemberships().

To list all channels, use GetChannels() instead.

Method signature​

This method takes the following parameters:

  • GetMemberships() (on the User object)

    1user.GetMemberships(
    2 string filter = "",
    3 string sort = "",
    4 int limit = 0,
    5 PNPageObject page = null
    6)
  • GetUserMemberships() (on the Chat object)

    1chat.GetUserMemberships(
    2 string userId,
    3 string filter = "",
    4 string sort = "",
    5 int limit = 0,
    6 PNPageObject page = null
    7)
  • GetMemberships() (on the Channel object)

    1channel.GetMemberships(
    2 string filter = "",
    3 string sort = "",
    4 int limit = 0,
    5 PNPageObject page = null
    6)
  • GetChannelMemberships() (on the Chat object)

    1chat.GetChannelMemberships(
    2 string channelId,
    3 string filter = "",
    4 string sort = "",
    5 int limit = 0,
    6 PNPageObject page = null
    7)

Input​

ParameterRequired for GetMemberships() on UserRequired for GetUserMemberships() on ChatRequired for GetMemberships() on ChannelRequired for GetChannelMemberships() on ChatDescription
userId
Type: string
Default:
n/a
YesNoNoNoID of the user for which you want to retrieve memberships.
channelId
Type: string
Default:
n/a
NoNoYesNoID of the channel for which you want to retrieve memberships.
filter
Type: string
Default:
empty string
NoNoNoNoExpression used to filter the results. Returns only these memberships whose properties satisfy the given expression. The filter language is defined here.
sort
Type: string
Default:
empty string
NoNoNoNoKey-value pair of a property to sort by, and a sort direction. Available options are id, name, and updated. Use asc or desc to specify the sorting direction, or specify null to take the default sorting direction (ascending). For example: {name: "asc"}. By default, the items are sorted by the last updated date.
limit
Type: int
Default:
0
NoNoNoNoNumber of objects to return in response.
page
Type: PNPageObject
Default:
null
NoNoNoNoObject used for pagination to define which previous or next result page you want to fetch.

Output​

TypeDescription
Task<ChatOperationResult<MembersResponseWrapper>>An awaitable Task with the object containing the filtered, sorted, and paginated list of memberships.

Sample code​

Find out which channels the support_agent_15 user is a member of.

1

Check membership​

Check whether a user is a member of a channel, or retrieve a specific membership object.

Method signature​

These methods take the following parameters:

  • HasMember() (on the Channel object)

    1channel.HasMember(string userId)
  • GetMember() (on the Channel object)

    1channel.GetMember(string userId)
  • IsMemberOn() (on the User object)

    1user.IsMemberOn(string channelId)
  • GetMembership() (on the User object)

    1user.GetMembership(string channelId)

Input​

* required
ParameterDescription
userId
Type: string
Default:
n/a
The user ID to check membership for.
channelId
Type: string
Default:
n/a
The channel ID to check membership on.

Output​

MethodDescription
HasMember()
Type: Task<ChatOperationResult<bool>>
Returns true if the user is a member of the channel.
GetMember()
Type: Task<ChatOperationResult<Membership>>
Returns the Membership object for the user-channel relationship.
IsMemberOn()
Type: Task<ChatOperationResult<bool>>
Returns true if the user is a member of the channel.
GetMembership()
Type: Task<ChatOperationResult<Membership>>
Returns the Membership object for the user-channel relationship.

Sample code​

Check if a user is a member of some_channel using the Channel object.

1

Check if a user is a member of some_channel using the User object.

1

Get updates​

Receive updates when Membership objects are edited or deleted:

  • StreamUpdates() - monitors a single membership
  • OnUpdated and OnDeleted - event handlers for single membership changes
  • StreamUpdatesOn() - monitors multiple memberships
Method naming

Earlier versions used SetListeningForUpdates() to enable streaming. This method has been superseded by StreamUpdates(), though it remains available for backward compatibility.

Method signature​

These methods take the following parameters:

  • StreamUpdates()

    1membership.StreamUpdates(bool stream)
  • OnUpdated

    1// event on the Membership entity
    2public event Action<Membership> OnUpdated;
    3// needs a corresponding event handler
    4void EventHandler(Membership membership)
  • OnDeleted

    1// event on the Membership entity
    2public event Action OnDeleted;
    3// needs a corresponding event handler
    4void EventHandler()
  • StreamUpdatesOn() (static)

    1Membership.StreamUpdatesOn(
    2 List<Membership> memberships,
    3 Action<Membership> listener
    4)

Input​

ParameterRequired in StreamUpdates()Required in OnUpdated / OnDeletedRequired in StreamUpdatesOn()Description
stream
Type: bool
Default:
n/a
Yesn/an/aWhether to start (true) or stop (false) listening to Membership object updates.
memberships
Type: List<Membership>
Default:
n/a
NoNoYesList of Membership objects for which you want to get updates.
listener
Type: Action<Membership>
Default:
n/a
NoNoYesCallback that receives the specific membership that was updated.

Output​

These methods don't return a value. Updates are delivered through event handlers or callback functions.

Sample code​

Get updates on the first user membership.

1

Delete​

Remove a user-channel membership with Delete() on a Membership object. This permanently removes the membership from App Context.

Method signature​

This method has the following signature:

1membership.Delete()

Input​

This method doesn't take any parameters.

Output​

TypeDescription
Task<ChatOperationResult>Returned Task that you can await to get the result of the delete operation.

Sample code​

Delete the membership for support_agent_15 on the high-priority-incidents channel.

1

Update​

Update() updates the channel membership information for a given user.

Method signature​

This method takes the following parameters:

1membership.Update(ChatMembershipData membershipData)

Input​

* required
ParameterDescription
membershipData *
Type: ChatMembershipData
Default:
n/a
Any custom properties or metadata associated with the channel-user membership.

Output​

An awaitable Task<ChatOperationResult>.

Sample code​

Assign the premium-support role to support_agent_15 on the high-priority-incidents channel.

1

Was this page useful?

Last updated on