MILANO

19/03/2015

Introduction to Publications and Subscriptions, the magic behind the reactivity

Matteo Saporiti

Dev4Side .NET/JS/HTML/Whatever is needed developer

Yeah, I actually find the hat guy from xkcd likeable.

Session overview

  • The meteor way of sending data
  • Minimongo or how we have 'MongoDB' on client
  • How it all works
  • Customizations
  • Advanced uses

  • How publications work under the hood

Normal WEBSITE (json api)

  1. Client (browser) starts request
  2. Server handles the requeest and sends data formatted in JSON
  3. Client library inserts the data in the page

Meteor

  1. At load meteor start syncing data with the client
  2. The data is already on the client when it needs to be displayed (prefetch)
  3. The client can render the data without the need of a round-trip to the server
  4. Operations can happen both on client and server (latency compensation)

Minimongo

  1. Minimongo is a JS library that emulates nearly all the possible operations you can do on collections (only some geospatials query are excluded).
  2. The raw data are transferred from the server (real MongoDB) to the client via DDP (Distributed Data Protocol) using a WebSocket
  3. Minimongo cursors are reactive data sources
  4. You can work seamlessly and simultaneously on both Client Collections (Minimongo) and Server Collections (MongoDB)

client-server architecture

Server

Client

__________________________

__________________________

__________________________

DDP

MongoDB

 

Meteor.publish()

Minimongo

 

Meteor.subscribe()

How it works

Example 1

A basic publication and subscription

  1. Publications are the way the server exposes the Data that the client may need
  2. Subsciptions are how the client subscribes to the publications and let Meteor sync the data between the client and server.

things to note

  • Subscription reactivity, polling vs oplog
  • Observers and their reuse

Customizations

Example 2

Send only the data the client needs.

Example 3

Send only the object fields that the client needs

BEWARE

Multiple publications over the same collections may lead to unexpected results because the data are merged in the client.

This is because of the mergebox.

Advanced Uses

Example 4

Hide some data to non registered users.

It's possible to make publications smarter and differentiate the data that are sent to the client.

BEWARE

You should try to avoid having a publication with multiple possible values in queries.
Meteor does reuse the observer for successive requests, having different query make this optimization impossible and may lead to slowdowns.

Under the Hood

Example 5

How a publication works internally and some nice tricks that you can easily accomplish.

Bonus point

All the customizations do not change the query, so you can actually do this without having an impact on the performance.

So Long, and Thanks for All the Fish

The code used in the presentation is available at
https://github.com/matteosaporiti/Meetup_19_03

Slides will be available later on.