UP101: Publish/Subscribe Basics · 2020-02-04 · Lesson 2: Setting Up Pub/Sub Messaging. Import...

Post on 19-Jul-2020

3 views 0 download

transcript

UP101: Publish/Subscribe Basics

Lesson 1: Introduction to Publish and Subscribe Messaging

Overview

1.  Fundamental elements: Keys? Channels? Messages?

2.  How to use these elements.

3.  How to import and use the PubNub Pub/Sub API.

4.  Demo.

Three Steps of Message Delivery

1.  Client requests to publish to PubNub.

2.  PubNub sends requested message to subscribers.

3.  Active subscribers receive the published message in their

client application.

Elements of PubSub

1.  Keyset.

2.  Channels.

3.  Messages.

Keyset

1.  Publish/Subscribe Keys (and secret) form a single set.

2.  Each keyset is a unique namespace for your channels.

•  Example: A channel ‘blue’ under a given keyset is different

from channel ‘blue’ under a different keyset.

Channels

1.  Lightweight.

2.  Agnostic: all channels are equal, created for any purpose.

3.  Exists by virtue of using it. Publish to it- it exists.

4.  No need to prepare or declare properties for a channel

before use.

Messages

1.  JSON-format data for whatever you need to send.

2.  Can be any data.

3.  NOT binary, and less than 32 kb.

4.  JSON is recommended, but it can be any serializable

object (strings, numbers, etc.).

5.  Do NOT stringify the JSON.

Lesson 2: Setting Up Pub/Sub Messaging

Import PubNub

1.  Import the PubNub Library.

2.  This differs between SDKs.

3.  In Javascript, simply include code that links to our CDN.

Initialize PubNub

1.  Not a connection per se.

2.  An object used to invoke PN operations. An access point to PubNub.

3.  For Pub/Sub: initialize with Pub and Sub keys.

4.  Other parameters can be provided: these are covered later.

var pubnub = PUBNUB({publish_key: "publish_key",subscribe_key: "subscribe_key"});

A Note on the Sub Key

1.  Your Subscribe key is always required, even if you’re using

a device only to publish.

2.  It is the ‘lookup’ for your account.

Subscribe

1.  Call the pubnub.subscribe method.

2.  Include the channel name.

3.  Fill out the callback function: this is where messages are processed

and used by your app.

pubnub.subscribe({channel: "channel",callback: function(msg){console.log(msg);}});

Publish to PubNub

1.  Publishers populate channels with messages.

2.  A Publisher can also be a subscriber (they will receive their own

messages).

3.  Call the pubnub.publish method, and include channel name and

message.

pubnub.publish({channel: "channel",Message: "Hello, PubNub!",});

Rules and Guidelines

1.  You can only publish to one channel at once.

2.  You can subscribe to many channels at once (see UP105).

3.  Conversely, a single channel can have many subscribers.

About Channels

1.  Channels are flexible, according to your needs.

2.  One-to-one, one-to-many broadcast (one publisher, many

subscribers), fan-in (many publishers, one subscriber).

3.  PubNub doesn’t differentiate between channels.

4.  Channels are simply mechanisms for sending and

receiving messages.

Review

1.  Parts of Pub/Sub messaging: Keys, channels, messages.

2.  Setup: Import, Initialize, Subscribe, Publish.

3.  Details for your app lay in your message and message

processing structures.