---
source_url: https://www.pubnub.com/docs/chat/unity-chat-sdk/build/features/channels/membership
title: Manage the user-channel membership relationship
updated_at: 2026-06-19T11:35:21.529Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Manage the user-channel membership relationship

:::note Requires App Context
Enable [App Context](https://youtu.be/9UEoSlngpYI) for your keyset in the [Admin Portal](https://admin.pubnub.com/).
:::

A [Membership entity](https://www.pubnub.com/docs/chat/unity-chat-sdk/learn/chat-entities/membership) is created when a user [joins](https://www.pubnub.com/docs/chat/unity-chat-sdk/build/features/channels/join) or is [invited](https://www.pubnub.com/docs/chat/unity-chat-sdk/build/features/channels/invite) to a channel, and ends when the user [leaves](https://www.pubnub.com/docs/chat/unity-chat-sdk/build/features/channels/leave).

## Get membership

Get all channel [memberships](https://www.pubnub.com/docs/chat/unity-chat-sdk/learn/chat-entities/membership) for a user or channel with `GetMemberships()`.

To list all channels, use [GetChannels()](https://www.pubnub.com/docs/chat/unity-chat-sdk/build/features/channels/list) 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 = null6)
* GetUserMemberships() (on the Chat object) 1chat.GetUserMemberships(2 string userId,3 string filter = "",4 string sort = "",5 int limit = 0,6 PNPageObject page = null7)
* GetMemberships() (on the Channel object) 1channel.GetMemberships(2 string filter = "",3 string sort = "",4 int limit = 0,5 PNPageObject page = null6)
* GetChannelMemberships() (on the Chat object) 1chat.GetChannelMemberships(2 string channelId,3 string filter = "",4 string sort = "",5 int limit = 0,6 PNPageObject page = null7)

#### Input

| Parameter | Required for GetMemberships() on User | Required for GetUserMemberships() on Chat | Required for GetMemberships() on Channel | Required for GetChannelMemberships() on Chat | Description |
| --- | --- | --- | --- | --- | --- |
| userId | string | Optional |  | Yes | No | No | No | ID of the user for which you want to retrieve memberships. |
| channelId | string | Optional |  | No | No | Yes | No | ID of the channel for which you want to retrieve memberships. |
| filter | string | Optional | `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](https://www.pubnub.com/docs/general/metadata/filtering). |
| sort | string | Optional | `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 | int | Optional | `0` | No | No | No | No | Number of objects to return in response. |
| page | PNPageObject | Optional | `null` | No | No | No | No | Object used for pagination to define which previous or next result page you want to fetch. |

#### Output

| Type | Description |
| --- | --- |
| `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.

```csharp
using System;
using System.Linq;
using System.Threading.Tasks;
using PubnubApi;
using PubnubChatApi;
using UnityEngine;
// Configuration
PubnubChatConfig chatConfig = new PubnubChatConfig();
        
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
{
    SubscribeKey = "demo",
    PublishKey = "demo",
    Secure = true
};

// Initialize Unity Chat
var chatResult = await UnityChat.CreateInstance(chatConfig, pnConfiguration);
if (!chatResult.Error)
{
    chat = chatResult.Result;
}
var userResult = await chat.GetUser("support_agent_15");
if (userResult.Error)
{
    Debug.Log("User not found.");
    return;
}
var user = userResult.Result;
Debug.Log($"Found user with name {user.UserName}");
        
// Get the memberships of the user
var membershipsResult = await user.GetMemberships();

if (!membershipsResult.Error)
{
    Debug.Log($"Memberships of user {user.UserName}:");

    foreach (var membership in membershipsResult.Result.Memberships)
    {
        Debug.Log($"Channel ID: {membership.ChannelId}");
    }
}
```

## 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

| Parameter | Description |
| --- | --- |
| `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

| Method | Description |
| --- | --- |
| `HasMember()`Type: `Task<ChatOperationResult<bool>>` | Returns `true` if the user is a member of the channel. |
| `GetMember()`Type: `Task<ChatOperationResult<Membership>>` | Returns the [Membership](https://www.pubnub.com/docs/chat/unity-chat-sdk/learn/chat-entities/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](https://www.pubnub.com/docs/chat/unity-chat-sdk/learn/chat-entities/membership) object for the user-channel relationship. |

### Sample code

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

```csharp
using System;
using System.Linq;
using System.Threading.Tasks;
using PubnubApi;
using PubnubChatApi;
using UnityEngine;
// Configuration
PubnubChatConfig chatConfig = new PubnubChatConfig();
        
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
{
    SubscribeKey = "demo",
    PublishKey = "demo",
    Secure = true
};

// Initialize Unity Chat
var chatResult = await UnityChat.CreateInstance(chatConfig, pnConfiguration);
if (!chatResult.Error)
{
    chat = chatResult.Result;
}
var getChannel = await chat.GetChannel("some_channel");
if (getChannel.Error)
{
    Debug.Log("Channel not found.");
    return;
}
var channel = getChannel.Result;
        
// Check for and then get the memberships of the user
var hasMember = await channel.HasMember("some_user");
if (!hasMember.Error && hasMember.Result)
{
    // If you call this method and they aren't a member then the operation result will contain an error message
    var getMember = await channel.GetMember("some_user");
}
```

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

```csharp
using System;
using System.Linq;
using System.Threading.Tasks;
using PubnubApi;
using PubnubChatApi;
using UnityEngine;
// Configuration
PubnubChatConfig chatConfig = new PubnubChatConfig();
        
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
{
    SubscribeKey = "demo",
    PublishKey = "demo",
    Secure = true
};

// Initialize Unity Chat
var chatResult = await UnityChat.CreateInstance(chatConfig, pnConfiguration);
if (!chatResult.Error)
{
    chat = chatResult.Result;
}
var userResult = await chat.GetUser("support_agent_15");
if (userResult.Error)
{
    Debug.Log("User not found.");
    return;
}
var user = userResult.Result;
Debug.Log($"Found user with name {user.UserName}");
        
// Check for and then get the memberships of the user
var isMember = await user.IsMemberOn("some_channel");
if (!isMember.Error && isMember.Result)
{
    // If you call this method and they aren't a member then the operation result will contain an error message
    var getMember = await user.GetMembership("some_channel");
}
```

## Get updates

Receive updates when [Membership objects](https://www.pubnub.com/docs/chat/unity-chat-sdk/learn/chat-entities/membership) are edited or deleted:

* `StreamUpdates()` - monitors a single membership
* `OnUpdated` and `OnDeleted` - event handlers for single membership changes
* `StreamUpdatesOn()` - monitors multiple memberships

:::tip 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 entity2public event Action<Membership> OnUpdated;3// needs a corresponding event handler4void EventHandler(Membership membership)
* OnDeleted 1// event on the Membership entity2public event Action OnDeleted;3// needs a corresponding event handler4void EventHandler()
* StreamUpdatesOn() (static) 1Membership.StreamUpdatesOn(2 List<Membership> memberships,3 Action<Membership> listener4)

#### Input

| Parameter | Required in `StreamUpdates()` | Required in `OnUpdated` / `OnDeleted` | Required 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](https://www.pubnub.com/docs/chat/unity-chat-sdk/learn/chat-entities/membership) for which you want to get updates. |
| `listener`Type: `Action<Membership>`Default: n/a | No | No | Yes | Callback 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.

```csharp
using System;
using System.Linq;
using System.Threading.Tasks;
using PubnubApi;
using PubnubChatApi;
using UnityEngine;
// Configuration
PubnubChatConfig chatConfig = new PubnubChatConfig();
        
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
{
    SubscribeKey = "demo",
    PublishKey = "demo",
    Secure = true
};

// Initialize Unity Chat
var chatResult = await UnityChat.CreateInstance(chatConfig, pnConfiguration);
if (!chatResult.Error)
{
    chat = chatResult.Result;
}
// reference the "support_agent_15" user
var userResult = await chat.GetUser("support_agent_15");
if (!userResult.Error)
{
    var user = userResult.Result;
    Debug.Log($"Found user with name {user.UserName}");
            
    // get the list of all user memberships
    var membershipsResponse = await user.GetMemberships();
            
    // extract the actual memberships from the response
    if (!membershipsResponse.Error)
    {
        var memberships = membershipsResponse.Result.Memberships;
        if (memberships.Any())
        {
            // get the first membership
            var firstMembership = memberships.First();
                    
            // output the first membership details
            Debug.Log($"First membership for user {user.UserName} is in channel {firstMembership.ChannelId}");
                    
            // start listening for updates on memberships
            firstMembership.StreamUpdates(true);
                    
            // attach an event handler for membership updates
            firstMembership.OnUpdated += OnMembershipUpdatedHandler;

            // example event handler for membership updates
            void OnMembershipUpdatedHandler(Membership updatedMembership)
            {
                Debug.Log($"Membership updated: {updatedMembership.ChannelId}");
            }
        }
        else
        {
            Debug.Log("The user 'support_agent_15' has no memberships.");
        }
    }
}
else
{
    Debug.Log("User 'support_agent_15' not found.");
}
```

## 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:

```csharp
membership.Delete()
```

#### Input

This method doesn't take any parameters.

#### Output

| Type | Description |
| --- | --- |
| `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.

```csharp
using System;
using System.Linq;
using System.Threading.Tasks;
using PubnubApi;
using PubnubChatApi;
using UnityEngine;
// Configuration
PubnubChatConfig chatConfig = new PubnubChatConfig();
        
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
{
    SubscribeKey = "demo",
    PublishKey = "demo",
    Secure = true
};

// Initialize Unity Chat
var chatResult = await UnityChat.CreateInstance(chatConfig, pnConfiguration);
if (!chatResult.Error)
{
    chat = chatResult.Result;
}
// reference the "support_agent_15" user
var userResult = await chat.GetUser("support_agent_15");
if (userResult.Error)
{
    Debug.Log("Couldn't find user!");
    return;
}
var user = userResult.Result;

// get the list of all user memberships and filter out the right channel
var membershipsWrapperResult = await user.GetMemberships(
    filter: "channel.id == 'high-priority-incidents'"
);

// get the membership and delete it
if(!membershipsWrapperResult.Error && membershipsWrapperResult.Result.Memberships.Any())
{
    var membership = membershipsWrapperResult.Result.Memberships[0];
    await membership.Delete();
}
```

## Update

`Update()` updates the channel [membership](https://www.pubnub.com/docs/chat/unity-chat-sdk/learn/chat-entities/membership) information for a given user.

### Method signature

This method takes the following parameters:

```csharp
membership.Update(ChatMembershipData membershipData)
```

#### Input

| Parameter | Description |
| --- | --- |
| `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.

```csharp
using System;
using System.Linq;
using System.Threading.Tasks;
using PubnubApi;
using PubnubChatApi;
using UnityEngine;
// Configuration
PubnubChatConfig chatConfig = new PubnubChatConfig();
        
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
{
    SubscribeKey = "demo",
    PublishKey = "demo",
    Secure = true
};

// Initialize Unity Chat
var chatResult = await UnityChat.CreateInstance(chatConfig, pnConfiguration);
if (!chatResult.Error)
{
    chat = chatResult.Result;
}
// reference the "support_agent_15" user
var userResult = await chat.GetUser("support_agent_15");
if (userResult.Error)
{
    Debug.Log("Couldn't find user!");
    return;
}
var user = userResult.Result;

// get the list of all user memberships and filter out the right channel
var membershipsWrapperResult = await user.GetMemberships(
    filter: "channel.id == 'high-priority-incidents'"
);

if(!membershipsWrapperResult.Error && membershipsWrapperResult.Result.Memberships.Any())
{
    var membership = membershipsWrapperResult.Result.Memberships[0];
    membership.MembershipData.CustomData["role"] = "premium-support";
    // add custom metadata to the user membership
    await membership.Update(membership.MembershipData);
}
```