2017-03-23 16:27:59 +09:00
# vulcan:subscribe
2016-08-07 19:26:08 +09:00
2017-03-24 10:35:19 +09:00
This optional package for Vulcan lets your users subscribe to the different domains (collections) of your application.
2016-08-07 19:26:08 +09:00
2016-08-16 17:04:04 +02:00
### Dependencies & usage
2017-03-23 16:27:59 +09:00
Explicit dependency on `vulcan:users` to enable permissions.
2016-08-07 19:26:08 +09:00
2017-03-23 16:27:59 +09:00
If `vulcan:posts` is enabled, your users will be able to subscribe to:
2016-08-16 17:04:04 +02:00
* new posts from users they follow (subscribed to)
* new comments on a post they are subscribed to
2016-08-07 19:26:08 +09:00
2017-03-23 16:27:59 +09:00
If `vulcan:categories` is enabled, your users will be able to subscribe to new posts in a category.
2016-08-07 19:26:08 +09:00
2016-08-16 17:04:04 +02:00
### Basic usage
2016-08-07 19:26:08 +09:00
2016-08-16 17:04:04 +02:00
This package gives you access to several methods of the type `collection.subscribe` & `collection.unsubscribe` . Default group of users can subscribe to any activated domain (see above) with the following methods (action) :
```
users.subscribe
users.unsubscribe
posts.subscribe
posts.unsubscribe
categories.subscribe
categories.unsubscribe
```
2016-08-07 19:26:08 +09:00
2016-08-16 17:04:04 +02:00
This package also provides a reusable component called `SubscribeTo` to subscribe to an document of collection.
2016-08-07 19:26:08 +09:00
2017-02-07 12:51:11 +01:00
This component takes the `document` as a props. It can trigger any method described below:
2016-08-07 19:26:08 +09:00
2016-08-16 17:04:04 +02:00
```jsx
// for example, in PostItem.jsx
2017-02-07 12:51:11 +01:00
< Components.SubscribeTo document = {post} / >
2016-08-07 19:26:08 +09:00
2016-08-16 17:04:04 +02:00
// for example, in UsersProfile.jsx
2017-02-07 12:51:11 +01:00
< Components.SubscribeTo document = {user} / >
2016-08-16 17:04:04 +02:00
// for example, in Category.jsx
2017-02-07 12:51:11 +01:00
< Components.SubscribeTo document = {category} / >
2016-08-16 17:04:04 +02:00
```
### Extend to other collections than Users, Posts, Categories
2017-09-22 09:54:44 +02:00
This package exports a function called `subscribeMutationsGenerator` that takes a collection as an argument and create the associated methods code :
2016-08-16 17:04:04 +02:00
```js
// in my custom package
2017-09-22 09:54:44 +02:00
import subscribeMutationsGenerator from 'meteor/vulcan:subscribe';
2016-08-16 17:04:04 +02:00
import Movies from './collection.js';
2017-02-07 12:51:11 +01:00
// the function creates the code and give it to the graphql server
2017-09-22 09:54:44 +02:00
subscribeMutationsGenerator(Movies);
2016-08-16 17:04:04 +02:00
```
2017-09-22 09:54:44 +02:00
This will creates for you the mutations `moviesSubscribe` & `moviesUnsubscribe` than can be used in the `SubscribeTo` component:
2016-08-16 17:04:04 +02:00
```jsx
// in my custom component
2017-02-07 12:51:11 +01:00
< Components.SubscribeTo document = {movie} / >
2016-08-16 17:04:04 +02:00
```
You'll also need to write the relevant callbacks, custom fields & permissions to run whenever a user is subscribed to your custom collection's item. See these files for inspiration.
*Note: it's more or less always the same thing*
2017-03-24 10:35:19 +09:00
* Custom fields: https://github.com/TelescopeJS/Telescope/blob/devel/packages/vulcan-subscribe/lib/custom_fields.js#L47 -L75
* Callbacks: https://github.com/TelescopeJS/Telescope/blob/devel/packages/vulcan-subscribe/lib/callbacks.js#L13 -L36
* Permissions: https://github.com/TelescopeJS/Telescope/blob/devel/packages/vulcan-subscribe/lib/permissions.js
2016-08-16 17:04:04 +02:00
### Reusable component to show a list of subscribed items
There was formerly a component that showed a list of subscribed posts. While reducing the depencies to other packages, it broke. It's on the roadmap to re-enable it. Feel free to discuss about it [on the Slack channel ](http://slack.telescopeapp.org/ ) if you want to build it!
Original PR & discussion can be found here: https://github.com/TelescopeJS/Telescope/pull/1425