On this page

Manage the user-channel membership relationship

Requires App Context

To set up and manage channel membership, you must enable App Context for your app's keyset in the Admin Portal.

When a user joins a channel or gets invited to it, a membership relationship between that user and the channel is created (Membership entity). The membership ends when this user leaves the channel.

Read on to learn how to check and update user-channel membership.

Get membership

There are two GetMemberships() methods that return a wrapper containing a list of all channel memberships of a given user.

Both of these methods give the same output. The only difference is that you call a given method either on the User or the Channel object.

To get a list of all existing channels, use the GetChannels() method.

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
Yes
No
No
No
ID of the user for which you want to retrieve memberships.
channelId
Type: string
Default:
n/a
No
No
Yes
No
ID of the channel for which you want to retrieve memberships.
filter
Type: string
Default:
empty string
No
No
No
No
Expression 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
No
No
No
No
Key-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
No
No
No
No
Number of objects to return in response.
page
Type: PNPageObject
Default:
null
No
No
No
No
Object 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

Get updates

These methods let you receive updates when specific user-channel Membership objects are edited (status, type, custom data) on other clients:

  • StreamUpdates() — listen for Membership object update information.
  • OnMembershipUpdated and OnUpdate — add event handlers to a single Membership object update.
  • StreamUpdatesOn() — add a single event handler to each Membership object update from the provided list.
Membership changes

These methods notify you about field changes (e.g., metadata, status) for existing memberships, not new membership additions or removals.

Change types

For details on ChatEntityChangeType values (Updated and Deleted) and their meaning, see Change types.

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)
  • OnMembershipUpdated

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

    1// event on the Membership entity
    2public event Action<Membership, ChatEntityChangeType> OnUpdate;
    3// needs a corresponding event handler
    4void EventHandler(Membership membership, ChatEntityChangeType changeType)
  • StreamUpdatesOn() (static) - returns all memberships

    1Membership.StreamUpdatesOn(
    2 List<Membership> memberships,
    3 Action<List<Membership>> listener
    4)
  • StreamUpdatesOn() (static) - returns individual membership with change type

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

Input

ParameterRequired in StreamUpdates()Required in OnMembershipUpdated / OnUpdateRequired in StreamUpdatesOn()Description
stream
Type: bool
Default:
n/a
Yes
n/a
n/a
Whether to start (true) or stop (false) listening to Membership object updates.
memberships
Type: List<Membership>
Default:
n/a
No
No
Yes
List of Membership objects for which you want to get updates.
listener
Type: Action<List<Membership>>
Default:
n/a
No
No
Yes (first overload)
Callback that receives the complete list of all memberships being monitored each time any change occurs.
listener
Type: Action<Membership, ChatEntityChangeType>
Default:
n/a
No
No
Yes (second overload)
Callback that receives the specific membership that was updated and the type of change.
changeType
Type: ChatEntityChangeType
Default:
n/a
n/a
No (only in OnUpdate)
No (only in second overload)
Enum indicating the type of change: Updated or Deleted.

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

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

Last updated on