Getting started
PubSub provides connectivity with cloud-based message-oriented middleware. You can use PubSub to pass messages between your app instances and your app's backend creating real-time interactive experiences.
PubSub is available with AWS IoT.
Installation and Configuration
AWS IoT
When used with AWSIoTDataManager
, PubSub is capable of signing request according to Signature Version 4.
The Podfile
that you configure to install the AWS Mobile SDK must contain the AWSIoT
pod:
platform :ios, '9.0'
target :'YOUR-APP-NAME' do use_frameworks!
pod 'AWSIoT' # other pods
end
Run pod install --repo-update
before you continue.
To use in your app, import the following:
import AWSIoT
Define your unique client ID and endpoint (incl. region) in your configuration:
// Initialize the AWSIoTDataManager with the configurationlet iotEndPoint = AWSEndpoint( urlString: "wss://xxxxxxxxxxxxx-ats.iot.<YOUR-AWS-REGION>.amazonaws.com/mqtt")let iotDataConfiguration = AWSServiceConfiguration( region: AWSRegionType.<YOUR-AWS-REGION>, endpoint: iotEndPoint, credentialsProvider: AWSMobileClient.default())
AWSIoTDataManager.register(with: iotDataConfiguration!, forKey: "MyAWSIoTDataManager")let iotDataManager = AWSIoTDataManager(forKey: "MyAWSIoTDataManager")
Create IAM policies for AWS IoT
To use PubSub with AWS IoT, you will need to create the necessary IAM policies in the AWS IoT Console, and attach them to your Amazon Cognito Identity.
Go to IoT Core and choose Secure from the left navigation pane, and then Policies from the dropdown menu. Next, click Create. The following myIOTPolicy
policy will allow full access to all the topics.
Attach your policy to your Amazon Cognito Identity
To attach the policy to your Cognito Identity, begin by retrieving the Cognito Identity Id
from AWSMobileClient
.
AWSMobileClient.default().getIdentityId();
Then, you need to attach the myIOTPolicy
policy to the user's Cognito Identity Id with the following AWS CLI command:
aws iot attach-principal-policy --policy-name 'myIOTPolicy' --principal '<YOUR_COGNITO_IDENTITY_ID>'
Follow the instructions here if you are looking to:
- Use AWS IoT for your Swift project using v2 of the Amplify Library for Swift
- Migrate to v2 of the Amplify Library for Swift and you currently have an AWS IoT implementation in your project.