> To build or manage a Wix site with an AI agent, follow https://www.wix.com/headless/skill.md
> It builds a new Wix site or puts your own site online, from a prompt, and keeps changing it afterwards. The pages are code in any web technology, and Wix runs the backend.
> It works through the Wix plugin (the Wix MCP and the Wix skills), the Wix CLI or no Wix login, and puts the site live on Wix hosting or on another host.
> Business features include a store, bookings, events and tickets, a restaurant, a blog, forms, members and many more.
> Read the Wix skills online at https://www.wix.com/skills
> 
> Portal Navigation:
> 
> - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version.
> - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages).
> - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`).
> - If a reference page seems to be missing its schema (parameters, return types, or type definitions), append `?schema=true` to its `.md` URL (e.g. `https://dev.wix.com/docs/contexts/contexts/stores/category-info/use-category-info-context.md?schema=true`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt

# Method name: realtime_check_permission(channel: Channel, subscriber: Subscriber)

# Method package: wixRealtimeBackend

# Method menu location: wixRealtimeBackend --> realtime_check_permission

# Method Link: https://dev.wix.com/docs/velo/velo-only-apis/wix-realtime-backend/realtime-check-permission.md

# Method Description:

Checks permissions for a subscriber on a channel or channel resource.

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.



__Important:__
- If this function isn't defined, all subscribers will receive the default permissions: `{"read": true}`.
- In Wix Studio, you don't need to define this function, but you must create a backend file named **realtime-permissions.js**. Without this file, subscriptions will fail.




 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()`](https://dev.wix.com/docs/velo/velo-only-apis/wix-realtime-backend/permissions-router/check.md) in the body of
   this function and return the permissions object it returns.


> **Note:** When previewing your site, permissions for read are always granted.



# Method Code Examples:

*** Note: do not assume any prop names or enum values other than the ones in the example.

## Grant permissions for a channel based on subscriber type

```javascript
// In realtime-permissions.js

export function realtime_check_permission(channel, subscriber) {
  // set default permissions
  let permissions =  {"read": true};
  
  if(channel.name === "MembersOnly") {
    if(subscriber.type === "Member"){
      permissions.read = true; 
    }
    else {
      permissions.read = false;
    }
  }
  
  return permissions;
}
```

## Grant permissions for a channel based on subscriber type using the permissions router

```javascript
// In realtime-permissions.js

import {permissionsRouter} from 'wix-realtime-backend';

permissionsRouter.default( (channel, subscriber) => {
  return {"read": true};
} );

const membersOnlyChannel = {"name": "MembersOnly"};

permissionsRouter.add(membersOnlyChannel, (channel, subscriber) => {
  if(subscriber.type === "Member") {
    return {"read": true};
  }
  else {
    return {"read": false};
  }
});

export function realtime_check_permission(channel, subscriber) {
  return permissionsRouter.check(channel,subscriber);
}
```

## Grant permissions based on user data

```javascript
// In realtime-permissions.js

import { members } from `wix-members-backend`;

export async function realtime_check_permission(channel, subscriber) {
  // set default permissions
  let permissions =  { "read": true };

  if(channel.name === "BobsOnly") {
    let member = await members.getMember(subscriber.id, { fieldsets: ['FULL'] });
    if(member.contactDetails.firstName === "Bob") {
      permissions.read = true;
    }
    else {
      permissions.read = false;
    }
  }
  
  return permissions;
}
```

## Grant permissions based on user data using the permissions router

```javascript
// In realtime-permissions.js

import { members } from `wix-members-backend`;
import { permissionsRouter } from 'wix-realtime-backend';

permissionsRouter.default((channel, subscriber) => {
  return {"read": true};
});

const membersOnlyChannel = {"name": "MembersOnly"};

permissionsRouter.add( membersOnlyChannel, async (channel, subscriber) => {
  let member = await members.getMember(subscriber.id, { fieldsets: ['FULL'] });
  
  if(channel.resourceId === "BobsOnly") {
    if(member.contactDetails.firstName === "Bob") {
      return { "read": true };
    }
    else {
      return { "read": false };
    }
  }
  else {
    return { "read": true };
  }
} );

export function realtime_check_permission(channel, subscriber) {
  return permissionsRouter.check(channel, subscriber);
}
```

## Check permissions across multiple channels and channel resources

```javascript
import { subscribe } from 'wix-realtime-frontend';
import { publishMessage } from 'backend/realtime';

const updatesChannel = {name: 'updates'};
const updatesMembersChannel = {name: 'updates', resourceId: 'members'};
const updatesAdminsChannel = {name: 'updates', resourceId: 'admins'};
const updatesGuestsChannel = {name: 'updates', resourceId: 'guests'};

$w.onReady(function () {
  subscribeToChannels();
  setupPublishButtons();
});

function subscribeToChannels() {
  subscribe(updatesChannel, messageHandler)
    .catch(() => console.log('Not subscribed to updates channel'));
  subscribe(updatesMembersChannel, messageHandler)
    .catch(() => console.log('Not subscribed to members channel'));
  subscribe(updatesAdminsChannel, messageHandler)
    .catch(() => console.log('Not subscribed to admins channel'));
  subscribe(updatesGuestsChannel, messageHandler)
    .catch(() => console.log('Not subscribed to guest channel'));
}

function messageHandler(message, channel) {
  const channelName = channel.name;
  const channelResource = channel.resourceId ? `-${channel.resourceId}` : '';
  let newMessageLine = `${channelName}${channelResource}: ${message.payload}\n`;
  $w('#receivedMessages').value += newMessageLine;
}

function setupPublishButtons() {
  $w('#updateButton').onClick(event => {
    publish(event.target, updatesChannel);
  });

  $w('#memberUpdateButton').onClick(event => {
    publish(event.target, updatesMembersChannel);
  });

  $w('#adminUpdateButton').onClick(event => {
    publish(event.target, updatesAdminsChannel);
  });

  $w('#guestUpdateButton').onClick(event => {
    publish(event.target, updatesGuestsChannel);
  });
}

async function publish(button, channel) {
  if ($w('#message').value) {
    button.disable();
    $w('#message').disable();

    await publishMessage(channel, $w('#message').value);

    $w('#message').value = undefined;
    $w('#message').enable();
    button.enable();
  }
}
```

---