This package is a template system to connect with Google Cloud using REST subscribe to topics. It uses the Pull Subscription approach for subscription.
Gcloud Setup Summary |
---|
Create Gcloud account |
Create a project in gcloud |
Create a topic(optional) |
Create a subscription for the desired topic |
Create a service account |
- Setup on Gcloud Detailed:
Following steps are assuming user already has an account and has access to Gcloud IoT Console.
- If you already have a project created and also have a topic which you wish to subscribe to, then directly go to step 3.
- Perform the Iot core quickstart: This will involve creating a device, device registry & a certificate, creating a topic and a subscription.
- Now that you have a topic in a project to subscribe to, create a pull subscription if you don't already have one. Follow this link to create a pull subscription.
- We use a service account to perform the pull subscription over HTTP REST. Follow the steps here to create a service account on google and generate a key. This key is used to generate access token that is used to perform the pull and acknowledgement. Things to note here are:
- default maximum validity for access token is 3600 seconds.
- The additional steps to increase the default maximum validity for access token are here
- Setup on ClearBlade:
- Get the Desired Credentials and update the Google IoT Configuration Library. The credentials include
PROJECT_ID
,MAX_MESSAGES_TO_PULL
,SUBSCRIPTION_NAME
,SUBSCRIPTION_SERVICE_ACCOUNT_PRIVATE_KEY
from the above Gcloud checklist summary. - Restart the pullSubscription service once the GoogleIoTConfig Library is updated.
- Update the
pullTimer
andupdateAccessTokenTimer
based on your use-case.
- Get the Desired Credentials and update the Google IoT Configuration Library. The credentials include
The accessTokenManager service listens on a timer to fetch a new access token. The reason we do that is the expiry time of an access token is limited, a maximum of 3600s. This service updates the AccessToken in a shared cache, which is used by the pullSubscription service to make a pull to the subscription.
Note: One can manually update the access token in the shared cache, by simply invoking the accessTokenManager
service.
accessTokenManager
-- a service that uses the private for a service account along with other credentials in theGoogleIoTConfig
library to gain the access token and update the shared cache with the latest access_token. This service runs of a timer, updateAccessTokenTimer. The frequency of execution of this timer should be less than 3600s, since the access_token has a maximum expiry time of 3600s.pullSubscription
-- a stream service which regularly pulls on a topic. The determine of pull's is defined by the pullTimer. If the pullTimer fires every 20 seconds, then the service makes a pull to the topic on google every 20 seconds. It reads the accessToken from the shared cache which is updated regularly by theaccessTokenManager
service.
GoogleIoTConfig
-- a configuration library, which holds constants specific to Google IoT.
The timers can be updated based on your use-case:
pullTimer
- this timer defines the frequency of your pull.updateAccessTokenTimer
- this timer defines the frequency at which the access token gets updated.
AccessTokenCache
- this cache stores the latest and active access_token. The expiry time for this cache is set to 3600s.