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:
1{
2 "byId": {
3 "user_53bbe00387004010a8b9ad5f36bdd4a7": [
4 {
5 "id": "channel_ac4e67b98b34b44c4a39466e93e",
6 "custom": {
7 "admin": true
8 }
9 }
10 ]
11 }
12}
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.
1createMembershipReducer();
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:
1pubnub.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.
1fetchMemberships( request, [meta] );
fetchMemberships arguments
| Parameter | Description |
|---|---|
request *Type: FetchMembershipRequest | Memberships request parameter object. |
[meta]Type: object | Standard meta options object. |
fetchMemberships FetchMembershipRequest properties
| Property | Description |
|---|---|
uuid *Type: stringDefault: n/a | The ID of the user whose memberships you wish to retrieve. |
limitType: numberDefault: 100 | Maximum number of results to return per page |
filterType: stringDefault: n/a | Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here. |
sortType: stringDefault: n/a | Key-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'} |
pageType: objectDefault: n/a | To 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"} |
includeType: objectDefault: {customFields: false,channelFields: false,customChannelFields: false,totalCount: false} | Specifies whether to include custom fields in the response, and whether to include a total result count. |
fetchMemberships sample usage
1dispatch(fetchMemberships({
2 uuid: 'user_53bbe00387004010a8b9ad5f36bdd4a7',
3 limit: '2'
4}));
setMemberships
Set memberships of user to a list of channels.
1setMemberships( request, [meta] );
setMemberships arguments
| Parameter | Description |
|---|---|
request *Type: MembershipRequest | Membership request parameter object. |
[meta]Type: object | Standard meta options object. |
setMemberships MembershipRequest properties
| Property | Description |
|---|---|
uuid *Type: stringDefault: n/a | The ID of the user. |
channels *Type: Array of objectsDefault: n/a | Array of objects indicating the channels(s) to be joined, along with optional custom data.[{id: "channel-id-1"custom: { ... }},{id: "channel-id-2"custom: { ... }},] |
limitType: numberDefault: 100 | Maximum number of results to return per page |
filterType: stringDefault: n/a | Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here. |
sortType: stringDefault: n/a | Key-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'} |
pageType: objectDefault: n/a | To 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"} |
includeType: objectDefault: {customFields: false,channelFields: false,customChannelFields: false,totalCount: false} | Specifies whether to include custom fields in the response, and whether to include a total result count. |
setMemberships sample usage
1dispatch(setMemberships({
2 uuid: 'user_53bbe00387004010a8b9ad5f36bdd4a7',
3 channels: [ { id: 'channel_ac4e67b98b34b44c4a39466e93e' } ]
4}));
removeMemberships
Remove a user from a list of channels.
1removeMemberships( request, [meta] );
removeMemberships arguments
| Parameter | Description |
|---|---|
request *Type: MembershipRequest | Membership request parameter object. |
[meta]Type: object | Standard meta options object. |
removeMemberships MembershipRequest properties
| Property | Description |
|---|---|
uuid *Type: stringDefault: n/a | The ID of the user. |
channels *Type: Array of stringssDefault: n/a | Array of channels(s) to leave.["channel-id-1","channel-id-2"] |
limitType: numberDefault: 100 | Maximum number of results to return per page |
filterType: stringDefault: n/a | Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here. |
sortType: stringDefault: n/a | Key-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'} |
pageType: objectDefault: n/a | To 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"} |
includeType: objectDefault: {customFields: false,channelFields: false,customChannelFields: false,totalCount: false} | Specifies whether to include custom fields in the response, and whether to include a total result count. |
removeMemberships sample usage
1dispatch(removeMemberships({
2 uuid: 'user_53bbe00387004010a8b9ad5f36bdd4a7',
3 channels: [ 'channel_ac4e67b98b34b44c4a39466e93e' ]
4}));