Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: lesson should now be up-to-date

...

Code Block
[
  {
    "Rx_role":"thing",
    "Tx_role":"controller",
    "Tx":"Y3dpMrT12b5EzNKmdFsUKR",
    "Rx":"UHD1ku5PraSmRFq6zk9dwo"
  }
]

Accepting Subscriptions

Note
titleEngine compatibility

The portion of this lesson which follows refers to an older version of the io.picolabs.subcription ruleset

There is one step remaining in the manual subscription process. Just because one pico offers a subscription to another pico doesn't mean that target pico is forced to accept it. This is manifest by the "status" of the subscriptions. For the originating pico, the status is "outbound" (short for outbound, pending), and for the other pico, the status is "inbound" (i.e. inbound, pending).

In fact, after the pending subscriptions were created, the io.picolabs.subscription rule for wrangler/subscription raises the wrangler event "inbound_pending_subscription_added" passing along all of the attributes. It is up to your pico to react by either accepting or rejecting. This cookbook example shows how to write a rule that auto-approves subscriptions.

This shows the event attributes associated with the event wrangler:inbound_pending_subscription_added:

...

It is also possible to manually accept an inbound subscription request. Suppose, for the sake of example, that we later on create an new thing pico, named "Thing 3" and we want it to join the party. We install into it the rulesets io.picolabs.subscription and mischief.thing so now all that is left is getting it subscribed. If we started with the Mischief pico as above, the thing pico would automatically accept the subscription, so let's start instead with the thing pico. We would request a subscription by sending this event to the thing pico:

Code Block
http://localhost:8080/sky/event/Cgzd9288VuKkvZDhHkMoeb/subscr/wrangler/subscription?
wellKnown_Tx=ME9i5bo26xFPEUp9yBHUbq&
Rx_role=thing&
Tx_role=controller&
name=thing3&
channel_type=subscription

If we now want to see the inbound subscription requests of the mischief pico, we can use this URL:

Code Block
http://localhost:8080/sky/cloud/ME9i5bo26xFPEUp9yBHUbq/io.picolabs.subscription/inbound

and see the pending subscription, which matches the event attributes associated with the event wrangler:inbound_pending_subscription_added:

Code Block
[
  {"Rx_role":"controller",
    "Tx_role":"thing",
    "Tx":"WorT1aMaRxu39eW8Kk8ECy",
    "Rx":"4XB1c9YKvLnHygDa75VaYu"
  }
]

Generally, promiscuously approving pending subscriptions is a security risk, so most auto-approvals take place where something about the subscription requestor can be verified. For example, a child might be programmed to automatically approve subscription requests from its parent, after checking that the invitation came in on a channel that can be verified as being from the parent.

For now, to complete the manual process, we will send this event to the Thing1 Thing 3 pico (line break added for readability–your URL will be all in one line):

Code Block
http://localhost:8080/sky/event/cj0deky3l0007khpqdmo4037y4XB1c9YKvLnHygDa75VaYu/approveaccept/wrangler/pending_subscription_approval?
subscription_name=mischief:thing1Rx=4XB1c9YKvLnHygDa75VaYu

Checking the Thing1 Thing3 pico again, you will see that its one subscription now has the the status of "subscribed"is now established. At the same time, that subscription in the Mischief pico also now has the status of "subscribedestablished".

Deleting Subscriptions

You can cancel a subscription manually by sending an event to either of the picos involved in the subscription, in this example, the Thing1 Thing3 pico.

Code Block
http://localhost:8080/sky/event/cj0deky3l0007khpqdmo4037y4XB1c9YKvLnHygDa75VaYu/deldelete-subscription/wrangler/subscription_cancellation?
subscription_name=mischief:thing1



Endnotes

This page is inspired by (and often copied verbatim from) the original fourth lesson, (Classic) Lesson: Pico to Pico Subscriptions.

...