AngularJS V4 Publish/Subscribe Tutorial for Realtime Apps

Publish and Subscribe Overview

PubNub utilizes a Publish/Subscribe model for real-time data streaming and device signaling which lets you establish and maintain persistent socket connections to any device and push data to global audiences in less than ¼ of a second.

The atomic components that make up a data stream are API Keys, Messages, and Channels.

API KEYS

To build an application that leverages the PubNub Network for Data Streams with Publish and Subscribe, you will need PubNub API Keys which we provide when you Sign-Up.

You will need at the minimum a subscribeKey and publishKey. If a client will only subscribe, and not publish, then the client only need to initialize with the subscribeKey. For clients who will be publishing only, or publishing and subscribing (a client can both publish and subscribe), it will need to initialize with both the subscribeKey and the publishKey.

You only need to supply the publishKey to clients that will publish (send) data to your application over the PubNub network. A read-only client for example would not need to initialize with this key.

MESSAGES OVERVIEW

A message consists of a channel, and its associated data payload. A publishing client publishes messages to a given channel, and a subscribing client receives only the messages associated with the channels its subscribed to.

PubNub Message payloads can contain any JSON data including Booleans, Strings, Numbers, Arrays, and Objects. Simply publish the native type per your platform, and the clients will JSON serialize the data for you. Subscribers will automatically deserialize the JSON for you into your platform's associated native type.

You only need to supply the publishKey to clients that will publish (send) data to your application over the PubNub network. A read-only client for example would not need to initialize with this key.

DATA STREAMS CODE SAMPLES

PUBLISHING AND SUBSCRIBING REQUIRES ONLY A FEW SIMPLE-TO-USE APIS:

Include the PubNub library

  • init() - instantiate a PubNub instance.
  • subscribe() - additively subscribe to a specific channel.
  • publish() - send a message on a specific channel.
  • unsubscribe() - additively unsubscribe to a specific channel.

Send Your Feedback