Search.../

realtime_check_permission( )

Checks permissions for a subscriber on a channel or channel resource.

Description

The realtime_check_permission hook is triggered when a site visitor subscribes to a channel to check what permissions the subscriber will have.

The realtime_check_permission() function is not a function that you call from your code. You define the function in a file named realtime-permissions.js in your site's Backend section.

If this function is not defined, all subscribers will receive the default permissions, which are: {"read": true}

Implement permissions logic for channels and subscribers by either:

  • Including all permissions logic in the body of this function.
  • Use the PermissionsRouter to add permissions logic per channel. Then call check() in the body of this function and return the permissions object it returns.

Note: When previewing your site, permissions for read are always granted.

Syntax

function realtime_check_permission(channel: Channel, subscriber: Subscriber): Promise<ChannelPermissions> | ChannelPermissions

realtime_check_permission Parameters

NAME
TYPE
DESCRIPTION
channel
Channel

The channel, and optionally the resource, to check permissions for.

subscriber
Subscriber

The subscriber to check permissions for.

Returns

The permissions granted to the subscriber on the channel or channel resource.

Return Type:

Promise<ChannelPermissions>

 | 

ChannelPermissions

ChannelPermissions

ChannelPermissions

NAME
TYPE
DESCRIPTION
read
Optional
boolean

Whether the subscriber has read permissions. Defaults to true.

Was this helpful?

Grant permissions for a channel based on subscriber type

Copy Code
1// In realtime-permissions.js
2
3export function realtime_check_permission(channel, subscriber) {
4 // set default permissions
5 let permissions = {"read": true};
6
7 if(channel.name === "MembersOnly") {
8 if(subscriber.type === "Member"){
9 permissions.read = true;
10 }
11 else {
12 permissions.read = false;
13 }
14 }
15
16 return permissions;
17}
Grant permissions for a channel based on subscriber type using the permissions router

Copy Code
1// In realtime-permissions.js
2
3import {permissionsRouter} from 'wix-realtime-backend';
4
5permissionsRouter.default( (channel, subscriber) => {
6 return {"read": true};
7} );
8
9const membersOnlyChannel = {"name": "MembersOnly"};
10
11permissionsRouter.add(membersOnlyChannel, (channel, subscriber) => {
12 if(subscriber.type === "Member") {
13 return {"read": true};
14 }
15 else {
16 return {"read": false};
17 }
18});
19
20export function realtime_check_permission(channel, subscriber) {
21 return permissionsRouter.check(channel,subscriber);
22}
Grant permissions based on user data

Copy Code
1// In realtime-permissions.js
2
3import wixUsersBackend from `wix-users-backend`;
4
5export async function realtime_check_permission(channel, subscriber) {
6 // set default permissions
7 let permissions = {"read": true};
8
9 if(channel.name === "BobsOnly") {
10 let user = await wixUsersBackend.getUser(subscriber.id);
11 if(user.firstName === "Bob") {
12 permissions.read = true;
13 }
14 else {
15 permissions.read = false;
16 }
17 }
18
19 return permissions;
20}
Grant permissions based on user data using the permissions router

Copy Code
1// In realtime-permissions.js
2
3import wixUsersBackend from `wix-users-backend`;
4import {permissionsRouter} from 'wix-realtime-backend';
5
6permissionsRouter.default( (channel, subscriber) => {
7 return {"read": true};
8} );
9
10const membersOnlyChannel = {"name": "MembersOnly"};
11
12permissionsRouter.add( membersOnlyChannel, async (channel, subscriber) => {
13 let user = await wixUsersBackend.getUser(subscriber.id);
14
15 if(channel.resourceId === "BobsOnly") {
16 if(user.firstName === "Bob") {
17 return {"read": true};
18 }
19 else {
20 return {"read": false};
21 }
22 }
23 else {
24 return {"read": true};
25 }
26} );
27
28export function realtime_check_permission(channel, subscriber) {
29 return permissionsRouter.check(channel,subscriber);
30}