Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/tazaki-cc/nestjs-microservices-aws-iot

Nest.js Microservices with AWS IoT
https://github.com/tazaki-cc/nestjs-microservices-aws-iot

aws-iot-core mqtt

Last synced: about 1 month ago
JSON representation

Nest.js Microservices with AWS IoT

Awesome Lists containing this project

README

        

# @tazaki/nestjs-microservices-aws-iot

## Overview

This package provides a seamless wrapper for the `AWS IoT SDK` within Nest.js microservices, enabling easy integration with AWS IoT Core. With this, you can effortlessly publish and subscribe to MQTT topics using AWS IoT services in your Nest.js application.

## Features

> **Note:** This package is still under development. The initial release includes the following features:

- **Publish** messages to an MQTT topics
- **Subscribe** to an MQTT topics
- Simplified configuration through Nest.js dependency injection

## Installation

```bash
# for npm
npm install @tazaki/nestjs-microservices-aws-iot

# for yarn
yarn add @tazaki/nestjs-microservices-aws-iot

# for pnpm
pnpm add @tazaki/nestjs-microservices-aws-iot
```

## Usage

### Subscribing to an MQTT Topic

To subscribe to a topic, set up the microservice in your `main.ts` as follows:

```typescript
import { AwsIotServer } from '@tazaki/nestjs-microservices-aws-iot';
import type { AwsIotContext } from '@tazaki/nestjs-microservices-aws-iot';

const app = await NestFactory.createMicroservice(
AppModule,
{
strategy: new AwsIotServer({
hostname: '[your-iot].iot.[region].amazonaws.com',
certPath: './keys/cert.pem',
keyPath: './keys/private.key',
}),
},
);
```

Then, in your controller, listen to incoming messages from the topic:

```typescript
interface PayloadResponse {
hello: string;
}

...
@MessagePattern('awesomeTopic')
topic(@Payload() data: AwsIotPayload, @Ctx() context: AwsIotContext) {
console.log(data, context);
}

@MessagePattern('wildcardTopic/+/hello')
wildcard(@Payload() data: AwsIotPayload, @Ctx() context: AwsIotContext) {
console.log(data, context);
}
...
```

### Publishing a Message to an MQTT Topic

```typescript
import { AwsIotClient } from '@tazaki/nestjs-microservices-aws-iot';

export class AppService {
private client: AwsIotClient;

constructor() {
// Initialize the client
this.client = new AwsIotClient({
hostname: '[your-iot].iot.[region].amazonaws.com',
certPath: './keys/cert.pem',
keyPath: './keys/private.key',
});
}

public publishMessage() {
// Publish a message to the topic
this.client
.send('topic', {
message: 'Hello, World!',
})
.subscribe();
}
}
```

## Usage Notice

This package was created to share internal code and may be used as-is. Please note that no guarantees are made regarding the functionality or suitability of the code in all environments.

## Additional Resources

For more details on using Nest.js, refer to the official documentation: [Nest.js Microservices Overview](https://docs.nestjs.com/microservices/basics).