{"id":188560,"date":"2021-06-27T12:13:22","date_gmt":"2021-06-27T19:13:22","guid":{"rendered":"https:\/\/m2msupport.net\/m2msupport\/?page_id=188560"},"modified":"2021-06-27T12:13:23","modified_gmt":"2021-06-27T19:13:23","slug":"receive-subscribed-messages-in-url-endpoint-from-google-cloud-platform-gcp","status":"publish","type":"page","link":"https:\/\/m2msupport.net\/m2msupport\/receive-subscribed-messages-in-url-endpoint-from-google-cloud-platform-gcp\/","title":{"rendered":"Receive subscribed messages in URL endpoint  from Google Cloud Platform (GCP)"},"content":{"rendered":"<p>Subscribers get messages published to a topic by subscribing to topic subscriptions. A push subscription can be setup to receive the messages in a URL endpoint from GCP.<\/p>\n<p><a href=\"https:\/\/m2msupport.net\/m2msupport\/download-iot-cloud-tester\/\">IoT Cloud Tester<\/a>&nbsp; application provides an easy interface to setup the push endpoint for a subscription.<\/p>\n<h1>To receive subscribed messages in URL endpoint from GCP,<\/h1>\n<ul>\n<li><a href=\"https:\/\/m2msupport.net\/m2msupport\/create-subscription-to-topic-in-google-cloud-platform-gcp\/\">Create a push subscription&nbsp;<\/a><\/li>\n<li>Publish message to the topic in the push subscription.<\/li>\n<li>Process the message received in the URL endpoint.<\/li>\n<li>In this below example,&nbsp; the push subscription (subscription_962446) was created for the topic &#8216;device_enviornment&#8217;.The push endpoint was setup as &#8216;https:\/\/m2msupport.net\/m2msupport\/process_push.php&#8217;. Any message published to the topic&nbsp;&#8216;device_enviornment&#8217; will be sent to that endpoint.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/m2msupport.net\/m2msupport\/wp-content\/uploads\/2021\/06\/create_subscription_push.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-188544\" src=\"https:\/\/m2msupport.net\/m2msupport\/wp-content\/uploads\/2021\/06\/create_subscription_push.png\" alt=\"\" width=\"902\" height=\"732\" srcset=\"https:\/\/m2msupport.net\/m2msupport\/wp-content\/uploads\/2021\/06\/create_subscription_push.png 902w, https:\/\/m2msupport.net\/m2msupport\/wp-content\/uploads\/2021\/06\/create_subscription_push-300x243.png 300w, https:\/\/m2msupport.net\/m2msupport\/wp-content\/uploads\/2021\/06\/create_subscription_push-768x623.png 768w, https:\/\/m2msupport.net\/m2msupport\/wp-content\/uploads\/2021\/06\/create_subscription_push-600x487.png 600w\" sizes=\"auto, (max-width: 902px) 100vw, 902px\" \/><\/a><\/p>\n<p>In the &#8216;Publish&#8217; tab, device &#8216;dev_70234&#8217; is enabled to publish messages to the topic &#8216;device_enviornment&#8217;. IoT Cloud Tester application will generate data based on the data model &#8216;device_data&#8217;. Google Cloud forwards those messages to the endpoint &#8216;https:\/\/m2msupport.net\/m2msupport\/process_push.php&#8217; which parses the message and writes to a text file.<\/p>\n<p><a href=\"https:\/\/m2msupport.net\/m2msupport\/wp-content\/uploads\/2021\/06\/push_example.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-188561\" src=\"https:\/\/m2msupport.net\/m2msupport\/wp-content\/uploads\/2021\/06\/push_example.png\" alt=\"\" width=\"1758\" height=\"1023\" srcset=\"https:\/\/m2msupport.net\/m2msupport\/wp-content\/uploads\/2021\/06\/push_example.png 1758w, https:\/\/m2msupport.net\/m2msupport\/wp-content\/uploads\/2021\/06\/push_example-300x175.png 300w, https:\/\/m2msupport.net\/m2msupport\/wp-content\/uploads\/2021\/06\/push_example-768x447.png 768w, https:\/\/m2msupport.net\/m2msupport\/wp-content\/uploads\/2021\/06\/push_example-1024x596.png 1024w, https:\/\/m2msupport.net\/m2msupport\/wp-content\/uploads\/2021\/06\/push_example-600x349.png 600w\" sizes=\"auto, (max-width: 1758px) 100vw, 1758px\" \/><\/a><\/p>\n<p>Here is the PHP code example that processes the message from the Google Cloud Platform<\/p>\n<p><em>&lt;?php<\/em><\/p>\n<p><em>\/\/Read the push JSON message from Google Cloud<\/em><br \/>\n<em>$json = file_get_contents(&#8216;php:\/\/input&#8217;);<\/em><\/p>\n<p><em>\/\/ Converts it into a PHP object<\/em><br \/>\n<em>$output = json_decode($json,TRUE);<\/em><\/p>\n<p><em>$file = &#8216;push_messages.txt&#8217;;<\/em><\/p>\n<p><em>\/\/ Open the file to get existing content<\/em><br \/>\n<em>$current = file_get_contents($file);<\/em><\/p>\n<p><em>\/\/Add the parsed elements to the text file<\/em><br \/>\n<em>$current .= &#8220;JSON Post from Google Cloud&#8221;.&#8221;\\n&#8221;.$json.&#8221;\\n&#8221;;<\/em><br \/>\n<em>$current .= &#8220;Device Id &#8211; &#8220;.$output[&#8216;message&#8217;][&#8216;attributes&#8217;][&#8216;deviceId&#8217;].&#8221;\\n&#8221;;<\/em><br \/>\n<em>$current .= &#8220;Message data &#8211; &#8220;.base64_decode($output[&#8216;message&#8217;][&#8216;data&#8217;]).&#8221;\\n&#8221;;<\/em><br \/>\n<em>$current .= &#8220;Subscription ID &#8211; &#8220;.$output[&#8216;subscription&#8217;].&#8221;\\n\\n&#8221;;<\/em><\/p>\n<p><em>\/\/ Write the contents back to the file<\/em><br \/>\n<em>file_put_contents($file, $current);<\/em><\/p>\n<p><em>?&gt;<\/em><\/p>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div class=\"video-responsive\"><iframe loading=\"lazy\" id=\"youTubePlayer\" src=\"https:\/\/www.youtube.com\/embed\/5zM1KU81UvM?hd=1\" width=\"750\" height=\"421\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Subscribers get messages published to a topic by subscribing to topic subscriptions. A push subscription can be setup to receive the messages in a URL endpoint from GCP. IoT Cloud Tester&nbsp; application provides an easy interface to setup the push &hellip; <a href=\"https:\/\/m2msupport.net\/m2msupport\/receive-subscribed-messages-in-url-endpoint-from-google-cloud-platform-gcp\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"iot_tutorial_template.php","meta":{"footnotes":""},"class_list":["post-188560","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/m2msupport.net\/m2msupport\/wp-json\/wp\/v2\/pages\/188560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/m2msupport.net\/m2msupport\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/m2msupport.net\/m2msupport\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/m2msupport.net\/m2msupport\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/m2msupport.net\/m2msupport\/wp-json\/wp\/v2\/comments?post=188560"}],"version-history":[{"count":1,"href":"https:\/\/m2msupport.net\/m2msupport\/wp-json\/wp\/v2\/pages\/188560\/revisions"}],"predecessor-version":[{"id":188563,"href":"https:\/\/m2msupport.net\/m2msupport\/wp-json\/wp\/v2\/pages\/188560\/revisions\/188563"}],"wp:attachment":[{"href":"https:\/\/m2msupport.net\/m2msupport\/wp-json\/wp\/v2\/media?parent=188560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}