Apache License 2.0

项目介绍

aws-samples/aws-iot-chat-example 是一个托管在 GitHub 上的开源项目,主要使用 JavaScript 编写,采用 Apache License 2.0 许可证,目前获得 567 个 Star、128 次 Fork。

仓库全名
aws-samples/aws-iot-chat-example
开源许可证
Apache License 2.0
Star 数
567
Fork 数
128
未关闭 Issue
18

README 摘要

AWS IoT Chat Application This is a React application demonstrating how to use the AWS IoT platform via MQTT over the WebSocket protocol to build a live chat application. The sample application serves as a starting point for users to build serverless projects with Amazon Cognito, AWS Lambda and Amazon DynamoDB. The project showcases the solution to common user questions such as how to authenticate IoT devices with…

完整内容见 GitHub 原始 README 或本页 。

常见问题

aws-iot-chat-example 用什么语言开发?

主要使用 JavaScript 编写,采用 Apache License 2.0 许可证,目前有 567 个 Star。

aws-iot-chat-example 可以免费商用吗?

仓库采用 Apache License 2.0 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 aws-iot-chat-example 的源码?

可以直接克隆仓库:git clone https://github.com/aws-samples/aws-iot-chat-example.git,默认分支为 master;安装与运行步骤见仓库 README。

aws-iot-chat-example 最近还在维护吗?

仓库最近一次更新为 2 周前,当前有 18 个未关闭 Issue;Star 数为 567,可作为社区活跃度的参考。

关于

💬 Chat application using AWS IoT platform via MQTT over the WebSocket protocol

描述取自 GitHub 官方 API,中文内容由 GitRead 中文社区 编辑团队整理。

简要分析

热度分
843
价值分
379
活跃状态
活跃

语言占比

CSS
HTML
JavaScript
Shell

README

AWS IoT Chat Application

tested with jest GitHub stars GitHub license Demo Online

This is a React application demonstrating how to use the AWS IoT platform via MQTT over the WebSocket protocol to build a live chat application. The sample application serves as a starting point for users to build serverless projects with Amazon Cognito, AWS Lambda and Amazon DynamoDB. The project showcases the solution to common user questions such as how to authenticate IoT devices with Amazon Cognito.

Table of Contents

Features

  • Register user
  • Login / Logout
  • Create room
  • Join room
  • Real-time chat
  • Unread message indicators

AWS Services Showcased

Architecture

Installation

Prerequisites:

Please note that this project has only been tested on macOS. Windows users may need to update some of the scripts. Pull requests are welcome to add Windows support.

  • Install and configure the AWS Command Line Interface
    • Ensure that the aws cli region is the same as what you define in api/config.yml
    • Please configure your output format to be json. Instructions
  • jq to process command-line JSON.
  • npm the node package manager (Node.js 20+ required)
  1. Clone the repository
$ git clone https://github.com/aws-samples/aws-iot-chat-example.git
  1. Deploy the backend

If you would like to enable login with Facebook and/or Google, please follow the Social Logins guide.

$ cd api
$ npm install
$ npx serverless@3 deploy --region us-east-1

If using a named AWS profile instead of environment variables:

$ npx serverless@3 deploy --region us-east-1 --profile your-profile-name

The Serverless Framework is a tool for deploying serverless architectures. The stack including Lambda functions and CloudFormation resources are defined in api/serverless.yml. The API uses esbuild to bundle the Lambda functions (no Babel/Webpack required).

After deploying the serverless application, two scripts are invoked by means of a lifecycle hook. The first, api/scripts/attachConfirmUserTrigger.sh is used to attach a Lambda function to the PreSignUp trigger on the Cognito User Pool.

The second script in client/scripts/setup.sh uses the AWS CLI to query the Cloudformation template output variables and stores them in the file client/src/config/config.json. This auto-generated file is then imported by client/src/config/index.js and used within the client side application.

  1. Run the frontend client
$ cd ../client
$ npm install
$ npm start

It is important to run the setup script so that the client knows the destination of the AWS IoT endpoint, API Gateway endpoint and other coniguration variables generated by serverless deploy.

  1. Open http://localhost:5173 and try it out!

Teardown

To tear down the stack, use the command npx serverless@3 remove --region us-east-1.

Application Walkthrough

Try the app on the Demo website

  1. Register Page /register

Register a user using Amazon Cognito User Pools. You can use user pools to add user registration and sign-in features to your apps.

Register

  1. Login page /login

Login a user using Amazon Cognito User Pools.

Alternatively, you can use Amazon Cognito Federated Identities to create unique identities for your users and federate them with identity providers such as Facebook and Google.

Login

  1. Rooms List /app/rooms

Displays the list of available rooms to join. If you are subscribed to a room, there will be an icon indication. The number of unread messages to subscribed rooms is also displayed. Lastly, on this screen you can create a room. The application will generate an appropriate topic name to publish on if your room name contains illegal characters.

All HTTP API endpoints are AWS Lambda functions invoked through Amazon API Gateway.

Rooms list

  1. Room /app/room/public/bad-programming-jokes

This is the main chat page where the real-time messaging is displayed. You will still be subscribed to other rooms as indicated by the unread message counters that increment when a message is received. Sending a message will dispatch a message to AWS IoT which in turn is responsible for fanning out the messages to all the subscribers of the room.

Architecture Walkthrough

Frontend

Redux Flows

Auth Flow Messaging Flow Rooms Screen Flow

Backend

To deploy the backend, this project uses the Serverless Framework which is backed by AWS CloudFormation. In the api/serverless.yml file, functions, events, resources and services are defined which Serverless uses to deploy the appropriate Lambda functions, API Gateway services and additional CloudFormation resources.

IoT Topics

MQTT (Message Queue Telemetry Transport) protocol uses a hierarchical structure to describe the topic space. Multi-level # wildcards and single-level + wildcards can be used in addition to topic level separators /.

In this application, topics are subscribed to using the form:

room/public/:roomName/+

This matches topics such as:

room/public/room/abc
room/public/another-room/123
room/public/another-room/us-west-2:7f86d9a8-b9f4-439e-bcf3-9756e4d2bbdf

Note the last example topic because this is the topic that users will publish to.

room/public/:roomName/:cognitoIdentityId

Based on the MQTT specification, a receiving client does not know who published the message without either including the message author in the message payload itself or by encoding the message author in the message topic.

This application opted for the second option by appending the cognito identity id to the end of the publishing topic. To prevent users from publishing to other user’s topics, the AWS Lambda function AttachPublicPublish creates a dynamic IoT policy that allows only that one identity to publish messages to their unique topic.

Testing

In both the api and client directories, npm run test can be used to run unit tests using jest

评论

暂无评论