Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add beforeUnsubscribe trigger doc #833

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions _includes/cloudcode/cloud-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -755,15 +755,36 @@ Parse.LiveQuery.on('error', (error) => {

*Available only on parse-server cloud code starting 4.3.0*

In some cases you may want to transform the incoming subscription query. Examples include adding an additional limit, increasing the default limit, adding extra includes or restricting the results to a subset of keys. You can do so with the `beforeSubscribe` trigger.
In some cases you may want to transform the incoming subscription query. Examples include adding an additional limit, increasing the default limit, adding extra includes or restricting the results to a subset of keys. You can do so with the `beforeSubscribe` trigger.

```javascript
Parse.Cloud.beforeSubscribe('MyObject', request => {
if (!request.user.get('Admin')) {
throw new Parse.Error(101, 'You are not authorized to subscribe to MyObject.');
}
let query = request.query; // the Parse.Query
query.select("name","year")
if (!request.user.get('Admin')) {
throw new Parse.Error(101, 'You are not authorized to subscribe to MyObject.');
}
let query = request.query; // the Parse.Query
query.select("name","year")
});
```


## beforeUnsubscribe

*Available only on parse-server cloud code starting 5.0.0*

In some cases you may want to stop an action initiated in the `beforeSubscribe` trigger.

```javascript
const liveQueries = {}
Parse.Cloud.beforeSubscribe('MyObject', request => {
liveQueries[request.requestId] = setInterval(() => { console.log(`Hello from ${request.requestId}`)})
});

Parse.Cloud.beforeUnsubscribe('MyObject', request => {
if (liveQueries[request.requestId]) {
clearInterval(liveQueries[request.requestId])
liveQueries[request.requestId] = null
}
});
```

Expand Down