(Classic) Lesson: Parent to Child Subscriptions
Learning Objectives
After completing this lesson, you will be able to do the following:
- Understand Parent-to-pico relationships and subscriptions
- Use the developer tools to manage pico subscriptions
- Link parent picos with a subscription programmatically to their children picos and have them interact.
Prerequisites
Completed (Classic) Quickstart lesson.
Be able to register and install rulesets
Completed Events & Queries lesson.
- Be able to create channels.
- Understand how events are raised on channels
Completed Pico-Based Systems.
- Understand what a Pico is.
- Be able to create new child Picos
Subscriptions
One of the most common subscription patterns is parent-to-child. Subscriptions are symmetric channel pairs that allow two picos to communication.
You need two rulesets one for parent and one for child. Parent creates child, child subscribes to parent.
-create child, you must provide name in event attribute.
rule createChild{ select when subscriptions parent_child_automate pre{ child_name = event:attr("name"); attr = {} .put(["Prototype_rids"],"<child rid as a string>") // ; separated rulesets the child needs installed at creation .put(["name"],child_name) // name for child_name .put(["parent_eci"],parent_eci) // eci for child to subscribe ; } { noop(); } always{ raise wrangler event "child_creation" attributes attr.klog("attributes: "); log("create child for " + child); } }
-send subscription request to parent from child
rule childToParent { select when wrangler init_events pre { // find parant // place "use module v1_wrangler alias wrangler_api" in meta block!! parent_results = wrangler_api:parent(); parent = parent_results{'parent'}; parent_eci = parent[0]; // eci is the first element in tuple attrs = {}.put(["name"],"Family") .put(["name_space"],"Tutorial_Subscriptions") .put(["my_role"],"Child") .put(["subscriber_role"],"Parent") .put(["subscriber_eci"],parent_eci.klog("target Eci: ")) .put(["channel_type"],"Pico_Tutorial") .put(["attrs"],"success") ; } { noop(); } always { raise wrangler event "subscription" attributes attrs; } }
-accept any subscription in parent pico
rule autoAccept { select when wrangler inbound_pending_subscription_added pre{ attributes = event:attrs().klog("subcription :"); } { noop(); } always{ raise wrangler event 'pending_subscription_approval' attributes attributes; log("auto accepted subcription."); } }
Copyright Picolabs | Licensed under Creative Commons.