Skip to content

Commit 4b67fab

Browse files
dblythyTomWFox
andauthored
Add beforeConnect / beforeSubscribe docs (#752)
* Add beforeConnect / beforeSubscribe docs * Update _includes/cloudcode/cloud-code.md Co-authored-by: Tom Fox <[email protected]> * Update _includes/cloudcode/cloud-code.md Co-authored-by: Tom Fox <[email protected]> * Update _includes/cloudcode/cloud-code.md Co-authored-by: Tom Fox <[email protected]> Co-authored-by: Tom Fox <[email protected]>
1 parent 868b0a3 commit 4b67fab

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

_includes/cloudcode/cloud-code.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,46 @@ Parse.Cloud.afterLogout(async request => {
598598
599599
# LiveQuery Triggers
600600
601+
## beforeConnect
602+
603+
*Available only on parse-server cloud code starting 4.3.0*
604+
605+
You can run custom Cloud Code before a user attempts to connect to your LiveQuery server with the `beforeConnect` method. For instance, this can be used to only allow users that have logged in to connect to the LiveQuery server.
606+
607+
```javascript
608+
Parse.Cloud.beforeConnect(request => {
609+
if (!request.user) {
610+
throw "Please login before you attempt to connect."
611+
}
612+
});
613+
```
614+
615+
In most cases, the `connect` event is called the first time the client calls `subscribe`. If this is your use case, you can listen for errors using this event.
616+
617+
```javascript
618+
Parse.LiveQuery.on('error', (error) => {
619+
console.log(error);
620+
});
621+
```
622+
623+
## beforeSubscribe
624+
625+
*Available only on parse-server cloud code starting 4.3.0*
626+
627+
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.
628+
629+
```javascript
630+
Parse.Cloud.beforeSubscribe('MyObject', request => {
631+
if (!request.user.get('Admin')) {
632+
throw new Parse.Error(101, 'You are not authorized to subscribe to MyObject.');
633+
}
634+
let query = request.query; // the Parse.Query
635+
query.select("name","year")
636+
});
637+
```
638+
639+
## onLiveQueryEvent
640+
601641
*Available only on parse-server cloud code starting 2.6.2*
602642
603643
Sometimes you may want to monitor Live Query Events to be used with a 3rd Party such as datadog. The `onLiveQueryEvent` trigger can log events triggered, number of clients connected, number of subscriptions and errors.

0 commit comments

Comments
 (0)