Integrating AWS Amplify with Eclipse Mosquitto for MQTT Messaging
Testing AWS Amplify's PubSub module against a local Eclipse Mosquitto broker before pointing it at AWS IoT Core.
Eclipse Mosquitto can serve as a local MQTT broker for testing AWS Amplify’s PubSub module before pointing it at a managed endpoint like AWS IoT Core.
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 an Angular Application
Create a new Angular application and install the 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.

Conclusion
Running a local Mosquitto broker with docker-compose and connecting it to an Angular application through Amplify’s PubSub module allows MQTT messages to be tested end to end without provisioning AWS IoT Core resources. The MqttOverWSProvider interface works with MQTT-over-WebSocket brokers, so the window.global polyfill and PubSub.subscribe('test-topic') call remain the same whether the application connects to Mosquitto at localhost:9001 or to AWS IoT Core.
Before moving beyond local testing, remove aws_appsync_dangerously_connect_to_http_endpoint_for_testing. This option permits the insecure connection used in the example and should not be enabled in production.
Related posts
Cognito SAML Sign-In with Microsoft Entra ID and Amplify
Configure SAML sign-in between an Amazon Cognito user pool and Microsoft Entra ID, then use it from a Next.js application with Amplify.
Angular Project Structure for Modularity and Reusability
Organize an Angular application into core, features, and shared directories with clear dependency boundaries.
Spying on Mock Object Properties in Jasmine
Working around Jasmine's "already been spied upon" error when spying on a mocked object's property with Object.getOwnPropertyDescriptor.
Sign in with Slack Using Cognito User Pools and OIDC
Federating Cognito user pools with Slack over OIDC and wiring "Sign in with Slack" into a Next.js app with Amplify.
Deploying FastAPI on AWS Lambda with Lambda Web Adapter
Containerizing a FastAPI backend and deploying it to a single Lambda function with Lambda Web Adapter and AWS CDK.
