Search⌘ K
AI Features

Subscribing to Data

Understand how to subscribe to data collections in a Meteor.js application by ensuring collections exist and are published to the client. Learn to use the useTracker hook in React to manage subscriptions and update the user interface reactively as data changes.

Before we can subscribe to a collection, the collection should exist, and it should be published to the client where we can subscribe to it.

The application below describes how we can subscribe to a collection.

Subscription checklist

The following checklist is adhered to when subscribing to data.

  • Make sure the collection exists.
  • Check that the collection is published to the server.
  • Subscribe to the data inside a useTracker function so that the user interface can be refreshed when the data changes.

Make sure the collection exists

The collection is defined under the api folder, which isinside the imports folder. Remember that all our application codes must be inside the imports folder.

Check that the collection is published to the server

Inside the imports folder, there’s a server folder, and inside of that server folder, there’s a publication.js file with the following content:

import {Meteor} from "meteor/meteor";
import { TasksCollection } from "../api/tasks";

Meteor.publish("tasks.allTask", function(){
    return TasksCollection.find();
});

We import the TasksCollection from its file. Next, we publish our collection, which has the name of tasks.allTask. The publication function returns all the TasksCollection in the system as a cursor.

Subscribe to data inside a useTracker function

Note: Open the App.js file inside the ui folder, which is located inside the imports folder. Uncomment the code before running the application.

Variables that our functions depend on are placed inside the array so that they trigger a recomputation if there are any changes. Because we have nothing to depend on, the empty array is passed. We’ll explore this in depth later in the React section of the course.

In line 8, we subscribe to the data by calling Meteor.subscribe("tasks.allTask"). The name “tasks.allTask” is the name of the subscription. It is important that the names of publications are unique.

The function returns an object with a property called loading, which is the state of the subscription and the tasks data.

In line 14, we check if our subscription is ready by checking the loading variable, which has a value that’s retrieved from !handle.ready(). If handle.ready() returns true, adding ! in front negates it. It’ll then return false.

We display a loading message on line 15 if our subscription isn’t ready.

We map over the tasks and, then, return all tasks along with their state of completion as finished or not finished.

# This file contains information which helps Meteor properly upgrade your
# app when you run 'meteor update'. You should check it into version control
# with your project.

notices-for-0.9.0
notices-for-0.9.1
0.9.4-platform-file
notices-for-facebook-graph-api-2
1.2.0-standard-minifiers-package
1.2.0-meteor-platform-split
1.2.0-cordova-changes
1.2.0-breaking-changes
1.3.0-split-minifiers-package
1.4.0-remove-old-dev-bundle-link
1.4.1-add-shell-server-package
1.4.3-split-account-service-packages
1.5-add-dynamic-import-package
1.7-split-underscore-from-meteor-base
1.8.3-split-jquery-from-blaze
Data subscription playground