• Stars
    star
    358
  • Rank 118,855 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Subscriptions Manager for Meteor

SubsManager Build Status

Subscriptions Manager for Meteor

This is a general-purpose subscriptions manager for Meteor. You can use Subscription Manager to cache subscriptions in the client side. This will help you to reduce the CPU usage of your app and improve the client side experience.

Why?

Normally you invoke subscriptions inside the template level or in the router level(in the case of Iron Router). So, when you are switching routes or changing templates/components, subscriptions will run again.

Normally, users go back and forth in your app in a single session. So, in each those route/page changes, app will re-subscribe data from the server. That's a waste and slow down your app in the client side.

Solution

Subscriptions Manager caches your subscriptions in the client side. So, when the user switching between routes, he will no longer have to wait. Also, Meteor won't need to re-send data that's already in the client.

In technical terms, Subscriptions Manager runs it's own Tracker.autorun computation internally. It does not interfere with Router or Templates. It works independently.

Subscriptions Manager does not cache your individual data. It tells Meteor to cache the whole subscription. So, your data will get updated in the background as usual.

Usage

Installation:

meteor add meteorhacks:subs-manager

Then create a new SubsManager instance:

PostSubs = new SubsManager();

Then instead of subscribing to Meteor.subscribe(), use PostSubs.subscribe(). Check this example:

Template.blogPost.onCreated(function() {
    var self = this;
    self.ready = new ReactiveVar();
    self.autorun(function() {
        var postId = FlowRouter.getQueryParam('postId');
        var handle = PostSubs.subscribe('singlePost', postId);
        self.ready.set(handle.ready());
    });
});

Here's how this works:

  • Let's say you visit page /posts?postId=abc.
  • Then Meteor will subscribe to the singlePost publication with abc as the postId.
  • Then let's imagine you visit to /posts?postId=bbc.
  • Just like above, it'll subscribe to the singlePost publication with bbc as the postId.
  • Now, let's say we go back to /posts?postId=abc. Then, subsManager won't fetch data again from the server. Subscription is already exists inside the client.

Check following links for more information:

Resetting

Sometime, we need to re-run our subscriptions due to various reasons.

Eg:- After a user has update the plan.

In those situations, you can try to reset Subscription Manager.

var subs = new SubsManager();

// later in some other place
subs.reset();

Clear Subscriptions

In some cases, we need to clear the all the subscriptions we cache. So, this is how we can do it.

var subs = new SubsManager();

// later in some other place
subs.clear();

Cache Control

Since now you are caching subscriptions, the Meteor server will also cache all your client data. But don't worry - that's not a huge issue.

But, you can control the caching behavior. Here's how to do it.

var subs = new SubsManager({
    // maximum number of cache subscriptions
    cacheLimit: 10,
    // any subscription will be expire after 5 minute, if it's not subscribed again
    expireIn: 5
});

Patterns for using SubsManager

Using a global Subscription Manager

You can create a global subscription manager as shown in the above example. By doing that, all your subscriptions are cached as a whole.

Using separate Subscription Managers

If you need more control over caching, you can create separate Subscription Managers for each set of subscriptions and manage them differently.

If you are using SubsManager inside a ReactComponents, this is the suggested way.

Using global ready checking

You can also check the ready status of all the subscriptions at once like this:

var subs = new SubsManager();
subs.subscribe('postList');
subs.subscribe('singlePost', 'id1');

Tracker.autorun(function() {
  if(subs.ready()) {
    // all the subscriptions are ready to use.
  }
});

Cache subscriptions you only need

With Subscription Manager you don't need to use it everywhere. Simply use it wherever you need without changing other subscriptions.

Limitations

Subscription Manager aims to be a drop-in replacement for Meteor.subscribe (or this.subscribe() in Iron Router). At the moment, the following functionality doesn't work.

  • onError and onReady callbacks (issue)

More Repositories

1

lokka

Simple JavaScript Client for GraphQL
JavaScript
1,532
star
2

flow-router

Carefully Designed Client Side Router for Meteor
JavaScript
1,089
star
3

mantra

Mantra - An Application Architecture for Meteor
Shell
980
star
4

fast-render

Render you app even before the DDP connection is live. - magic?
JavaScript
559
star
5

graphql-errors

Better error handling for GraphQL
JavaScript
253
star
6

blaze-layout

Layout Manager for Blaze (works well with Meteor FlowRouter)
JavaScript
198
star
7

npm-base

A base package for creating NPM packages with ES2015
JavaScript
161
star
8

meteor-debug

Full Stack Debugging Solution for Meteor
JavaScript
152
star
9

meteor-react-layout

Simple React Layout Manager for Meteor with SSR Support
JavaScript
138
star
10

meteor-dochead

Isomorphic way to manipulate document.head for Meteor apps
JavaScript
134
star
11

react-mounter

A simple way to mount React components
JavaScript
130
star
12

meteor-graphql

GraphQL Support for Meteor with Lokka
JavaScript
90
star
13

graphql-blog-schema

GraphQL Schema for a Blog App
CSS
84
star
14

graphqlify

Build GraphQL queries with JavaScript
JavaScript
56
star
15

react-simple-di

Simple Dependancy Injection Solution for React
JavaScript
56
star
16

react-stubber

Simple but useful stubbing solution React
JavaScript
50
star
17

blaze-plus

Adds Props and State Management functionality to Meteor's Blaze Template Engine.
JavaScript
50
star
18

meteor-login-state

Share Login State Between the Domain
JavaScript
45
star
19

meteor-reaktor

Easy to use React Frontend for FlowRouter
JavaScript
42
star
20

lokka-transport-http

HTTP Transport Layer for Lokka
JavaScript
41
star
21

lokka-transport-jwt-auth

Lokka GraphQL Transport with JWT Support
JavaScript
22
star
22

graphql-utils-js

A set of utilities for apps building with graphql-js
JavaScript
20
star
23

mongo-sharded-cluster

Shard MongoDB in the app layer
JavaScript
13
star
24

meteor-stripe-konnect

Stripe for Meteor Apps
JavaScript
12
star
25

mantra-tutor-lessons

11
star
26

kadiyadb

KadiraDB is a low level database for storing time series data
Go
10
star
27

node-eventloop-monitor

Simple way to monitor eventloop blockness in Node.js
JavaScript
9
star
28

node-base

Base app for all the server side node apps at Kadira
JavaScript
6
star
29

nofat

Set of tools to kick start server side ES2016 development
JavaScript
6
star
30

kadira-core

JavaScript
5
star
31

regex-query-filter

JavaScript
3
star
32

storybook-addon-hello

A simple hello-world example addon for storybook
JavaScript
3
star
33

lokka-transport-http-auth

HTTP Transport for Lokka with Basic Auth
JavaScript
2
star
34

meteor-string-highlighter

String Highlighter for Meteor Apps
JavaScript
2
star
35

fastcall

A fast bi-directional communication layer over tcp
Go
2
star
36

storybook-ping-receiver

Listen to anonymous usage pings sent from storybooks
JavaScript
2
star
37

url-mailer

JavaScript
2
star
38

kadira-debug-toy

MeteorToys integration for kadira:debug Package
JavaScript
2
star
39

kadira.io

Kadira Blog
JavaScript
2
star
40

go-tools

Go tools is a collection of re-usable go packages used by multiple KadiraHQ projects
Go
2
star
41

mongo-mask

Replaces data with masked values in mongo queries
JavaScript
1
star
42

kadiradb-node

NodejS client for KadiraDB metrics database.
JavaScript
1
star
43

kadiradb

A real time metrics database which uses KadiraDB under the hood
Go
1
star