Integrating AWS Amplify with Eclipse Mosquitto for MQTT Messaging
This note describes how to integrate AWS Amplify with Eclipse Mosquitto as an MQTT message broker.
This note describes how to integrate AWS Amplify with Eclipse Mosquitto as an MQTT message broker.
Setting Up Eclipse Mosquitto
Create a docker/docker-compose.yml file with the following content:
version: '3.4'
services: mosquitto: image: eclipse-mosquitto:1.6.9 ports: - 1883:1883 # MQTT - 9001:9001 # WebSocket volumes: - ./mosquitto:/mosquitto/configCreate a docker/mosquitto/mosquitto.conf file with the following content:
listener 1883listener 9001protocol websocketsStart the Mosquitto container:
docker-compose up -dBuilding Angular Application
Run the following commands to create a new Angular application and install necessary dependencies:
ng new angular-mosquitto-sampleUpdate src/polyfills.ts:
/** * ... */
/*************************************************************************************************** * APPLICATION IMPORTS */
// Add the following:(window as any).global = window;Edit src/app/app.component.html:
<h1>Received messages</h1><hr/><div *ngFor="let message of messages"> {{ message }}</div>Edit src/app/app.component.ts:
import { Component, OnInit } from '@angular/core';import { PubSub } from 'aws-amplify';import { MqttOverWSProvider } from '@aws-amplify/pubsub/lib/Providers';
@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss']})export class AppComponent implements OnInit { messages = [];
ngOnInit() { PubSub.addPluggable(new MqttOverWSProvider({ aws_pubsub_endpoint: 'ws://localhost:9001/', // Do not use SSL aws_appsync_dangerously_connect_to_http_endpoint_for_testing: true, }));
PubSub.subscribe('test-topic').subscribe(payload => { console.log(payload); this.messages.push(payload.value.message); }); }}Testing the Application
Install mqtt with the following command:
npm i --save-dev mqttPublish MQTT messages to the local Mosquitto broker:
mqtt pub \ -t 'test-topic' \ -h localhost \ -p 1883 \ -m '{"message": "Hello World"}'The Angular application should display the received message in real-time.

Related posts
Cognito SAML Sign-In with Azure Entra ID and Amplify
This note walks through SAML authentication with AWS Amplify, Cognito user pools, and Azure Entra ID.
Angular Project Structure for Modularity and Reusability
This note outlines an Angular folder structure with core, features, and shared modules to keep the app modular and reusable.
Spying on Mock Object Properties in Jasmine
This note describes how to spy on mock object properties in Jasmine.
Sign in with Slack Using Cognito User Pools and OIDC
This note describes how to federate Cognito user pools with Slack using OIDC to implement "Sign in with Slack".
Deploying FastAPI on AWS Lambda with Lambda Web Adapter
This example guides you through the process of developing API backends with FastAPI using Lambda Web Adapter.
