Integrating AWS Amplify with Eclipse Mosquitto for MQTT Messaging

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.

Takahiro Iwasa
2 min read

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:

docker/docker-compose.yml
version: '3.4'
services:
mosquitto:
image: eclipse-mosquitto:1.6.9
ports:
- 1883:1883 # MQTT
- 9001:9001 # WebSocket
volumes:
- ./mosquitto:/mosquitto/config

Create a docker/mosquitto/mosquitto.conf file with the following content:

listener 1883
listener 9001
protocol websockets

Start the Mosquitto container:

Terminal window
docker-compose up -d

Building Angular Application

Create a new Angular application and install the necessary dependencies:

Terminal window
ng new angular-mosquitto-sample

Update src/polyfills.ts:

src/polyfills.ts
/**
* ...
*/
/***************************************************************************************************
* APPLICATION IMPORTS
*/
// Add the following:
(window as any).global = window;

Edit src/app/app.component.html:

src/app/app.component.html
<h1>Received messages</h1>
<hr/>
<div *ngFor="let message of messages">
{{ message }}
</div>

Edit src/app/app.component.ts:

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:

Terminal window
npm i --save-dev mqtt

Publish MQTT messages to the local Mosquitto broker:

Terminal window
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 via docker-compose and wiring it into an Angular app through Amplify’s PubSub module made it possible to publish and receive MQTT messages end-to-end without any AWS IoT Core resources provisioned. Because PubSub talks to any MQTT-over-WebSocket broker through the same MqttOverWSProvider interface, the Angular side barely needs to know the difference — the polyfill for window.global and the PubSub.subscribe('test-topic') call stay the same whether the broker is Mosquitto on localhost:9001 or IoT Core. The one flag worth double-checking before moving past local testing is aws_appsync_dangerously_connect_to_http_endpoint_for_testing, since its name is a reasonably direct warning that it has no place in a production configuration.

About the author

Takahiro Iwasa

Takahiro Iwasa

Software Developer

This blog shares technical notes from hands-on projects—architecture, implementation, and AWS service integrations.