Memberships

When a user joins a channel, an association called a Membership is stored between the User and the Channel. Application users can immediately see which channels they belong to, join or leave channels to create these memberships, and access information about other users and channels.

State Shape

The data about Memberships is stored from the perspective of Users and Channels in a normalized structure. The following example shows the shape of Memberships in the store:

{
"byId": {
"user_53bbe00387004010a8b9ad5f36bdd4a7": [
{
"id": "channel_ac4e67b98b34b44c4a39466e93e",
"custom": {
"admin": true
}
}
]
}
}

Reducers

The PubNub Redux framework provides reducers your app can implement that respond to various actions that update the store. To track the state of a set of objects in the store, combine the reducers you want into the rootReducer for your app.

createMembershipReducer

createMembershipReducer instantiates a reducer in the store that responds to actions dispatched to update the state of a user's memberships, and all channel memberships, in the store.

createMembershipReducer();

Listeners

The PubNub Redux framework includes listeners that monitor PubNub events from the server and dispatch corresponding actions. All listeners are automatically invoked if your app registers the combined PubNub listener. You can register only specific listeners, or implement your own combine listeners function.

createMembershipListener

createMembershipListener registers a listener in the store that monitors Membership events.

A sample implementation of a single listener:

pubnub.addListener(createMembershipListener(store.dispatch));

Commands

The PubNub Redux framework provides commands that your app can dispatch to the store to be managed by the Thunk middleware. Commands interact with the PubNub API and dispatch basic actions which are then processed by reducers to update the state in the store.

fetchMemberships

Get the specified user's channel memberships.

fetchMemberships( request, [meta] );

fetchMemberships Arguments

ParameterTypeRequiredDescription
requestFetchMembershipRequestYesMemberships request parameter object.
[meta]objectOptionalStandard meta options object.

fetchMemberships FetchMembershipRequest Properties

PropertyTypeRequiredDefaultDescription
uuidstringYesThe ID of the user whose memberships you wish to retrieve.
limitnumberOptional100Maximum number of results to return per page
filterstringOptionaln/aExpression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here.
sortstringOptionaln/aKey-value pair of a property to sort by, and a sort direction. Available options are updated, channel.id, channel.name, and channel.updated.
Use asc or desc to specify sort direction, or specify null to take the default sort direction (ascending).
For example: {channel.name: 'asc'}
pageobjectOptionaln/aTo get the next set of results, specify next.
To get the previous set of results, specify prev.
If you specify both, prev is ignored.
{
    next: "next-page-id",
    prev: "prev-page-id"
}
includeobjectOptional{
    customFields: false,
    channelFields: false,
    customChannelFields: false,
    totalCountfalse
}
Specifies whether to include custom fields in the response, and whether to include a total result count.

fetchMemberships Sample Usage

dispatch(fetchMemberships({
uuid: 'user_53bbe00387004010a8b9ad5f36bdd4a7',
limit: '2'
}));

setMemberships

Set memberships of user to a list of channels.

setMemberships( request, [meta] );

setMemberships Arguments

ParameterTypeRequiredDescription
requestMembershipRequestYesMembership request parameter object.
[meta]objectOptionalStandard meta options object.

setMemberships MembershipRequest Properties

PropertyTypeRequiredDefaultDescription
uuidstringYesThe ID of the user.
channelsArray of objectsYesArray of objects indicating the channels(s) to be joined, along with optional custom data.

[
    {
        id: "channel-id-1"
        custom: { ... }
    },
    {
        id: "channel-id-2"
        custom: { ... }
    },
]
limitnumberOptional100Maximum number of results to return per page
filterstringOptionaln/aExpression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here.
sortstringOptionaln/aKey-value pair of a property to sort by, and a sort direction. Available options are updated, channel.id, channel.name, and channel.updated.
Use asc or desc to specify sort direction, or specify null to take the default sort direction (ascending).
For example: {channel.name: 'asc'}
pageobjectOptionaln/aTo get the next set of results, specify next.
To get the previous set of results, specify prev.
If you specify both, prev is ignored.
{
    next: "next-page-id",
    prev: "prev-page-id"
}
includeobjectOptional{
    customFields: false,
    channelFields: false,
    customChannelFields: false,
    totalCountfalse
}
Specifies whether to include custom fields in the response, and whether to include a total result count.

setMemberships Sample Usage

dispatch(setMemberships({
uuid: 'user_53bbe00387004010a8b9ad5f36bdd4a7',
channels: [ { id: 'channel_ac4e67b98b34b44c4a39466e93e' } ]
}));

removeMemberships

Remove a user from a list of channels.

removeMemberships( request, [meta] );

removeMemberships Arguments

ParameterTypeRequiredDescription
requestMembershipRequestYesMembership request parameter object.
[meta]objectOptionalStandard meta options object.

removeMemberships MembershipRequest Properties

PropertyTypeRequiredDefaultDescription
uuidstringYesThe ID of the user.
channelsArray of stringssYesArray of channels(s) to leave.

["channel-id-1","channel-id-2"]
limitnumberOptional100Maximum number of results to return per page
filterstringOptionaln/aExpression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here.
sortstringOptionaln/aKey-value pair of a property to sort by, and a sort direction. Available options are updated, channel.id, channel.name, and channel.updated.
Use asc or desc to specify sort direction, or specify null to take the default sort direction (ascending).
For example: {channel.name: 'asc'}
pageobjectOptionaln/aTo get the next set of results, specify next.
To get the previous set of results, specify prev.
If you specify both, prev is ignored.
{
    next: "next-page-id",
    prev: "prev-page-id"
}
includeobjectOptional{
    customFields: false,
    channelFields: false,
    customChannelFields: false,
    totalCountfalse
}
Specifies whether to include custom fields in the response, and whether to include a total result count.

removeMemberships Sample Usage

dispatch(removeMemberships({
uuid: 'user_53bbe00387004010a8b9ad5f36bdd4a7',
channels: [ 'channel_ac4e67b98b34b44c4a39466e93e' ]
}));
Last updated on