statement:
This article is transferred from the DEV Community website, and the article translation is provided by the developer community;
Click the link below to view the original English text:
Real-time communication between services or between people is difficult to achieve.
Fortunately, in today's cloud computing era, we have a variety of options to achieve reliable, WebSocket-based communication.
This article will introduce WebSocket in the Amazon cloud technology environment. What are the options? What are the pros and cons of each? Which architecture to deploy?
Let's take a look together.
Amazon API Gateway WebSocket
In December 2018, Amazon Cloud Technology released a new feature of Amazon API Gateway : support for WebSocket.
I remember being excited about it at the time. Since I tend to choose to develop a chat app when I learn a new programming language (because I find to-do list apps kind of boring), I immediately started reading the service documentation to understand this feature .
cons
After reading the documentation, my enthusiasm quickly faded.
- Support for broadcast behavior is far from ideal. As far as I know, there is no way to natively send messages to multiple connections with a single API call. The lack of native support for topics or rooms makes Amazon API Gateway WebSocket a less suitable choice when broadcasting functionality is required.
- The state of each connection needs to be tracked somehow . In my opinion, developers generally tend to use Amazon DynamoDB for this. If what you need is a fully managed solution, you will definitely be disappointed with this feature.
profit
However, this feature is not without benefits.
- Since you're probably already using Amazon API Gateway, adding WebSockets to your architecture greatly reduces the complexity of the entire process.
- With the direct integration capabilities of Amazon API Gateway, we can create granular and resilient WebSocket backends without the need for Lambda functions at all.
I took up the challenge myself. The results can be seen here . - The feature has a relatively small number of moving parts and, in my opinion , is very easy to pick up and simple to learn.
Architecture
I think Amazon API Gateway WebSocket is a great alternative to pooling . Depending on the traffic volume of the service, maintaining a WebSocket connection may be cheaper than making regular requests to the API. Here's a great resource on the topic .
Amazon AppSync GraphQL Subscriptions
Amazon AppSync
is a service for creating managed GraphQL APIs. As GraphQL gradually entered the mainstream, the service began to gain popularity rapidly.
Real-time capabilities are available through GraphQL subscriptions . Explaining exactly what a GraphQL subscription is is beyond the scope of this article, and if you're new to the technology, it's recommended to do your homework.
cons
- In my humble opinion, you may encounter many problems in the implementation process. Here's a great article on the topic .
- On the front end, you may have issues integrating the Amazon AppSync JavaScript client with one of your own GraphQL client libraries . As of this writing, there have been a number of related issues on GitHub related to integrations made by my favorite GraphQL client: apollo-client .
- This may be more obvious, but I still need to bring it up. As far as I know, this feature is only available in the context of GraphQL . It would be great to have a non-GraphQL version of the Amazon AppSync subscription. But maybe such a service already exists? Read on!
profit
- OMG! We can do a lot with Amazon AppSync. If all is well, the service can save you a lot of time . In my humble opinion, the basic knowledge required is easy to grasp, and if you are already familiar with GraphQL, it is easy to grasp the operation of AppSync subscriptions.
- Amazon AppSync subscriptions scale well . Scaling to millions of connections should generally not be a problem, according to Amazon Cloud . From personal experience, I've been able to push the service to about 500,000 connections without issue.
Architecture
Amazon AppSync offers a wealth of options. This service is ideal for most, if not all, scenarios that require a GraphQL subscription.
I recommend that you become familiar with the various Amazon AppSync use cases listed on the services page.
Amazon IoT Core MQTT
When I first heard about the Amazon IoT suite of services, the last thing that came to my mind was the rich and extensible support for WebSockets. After looking further, I was pleasantly surprised to find that the service even offers fully managed MQTT topics.
cons
- To fully understand WebSocket functionality, you first need to be familiar with the moving parts in many services . What is Device Shadow? Is some type of certificate required for authentication? What is the data endpoint? I asked myself these questions before trying to figure out how the service works.
- Maybe because I live in my own "community bubble", but I've seen very few tutorials on using the WebSocket feature in Amazon IoT Core . You may find it difficult to find answers to your questions. This is a blog post I referenced while learning about the service .
profit
- WebSocket via Amazon IoT Core MQTT is independent of the API architecture . Are you using your own GraphQL server? Or prefer to use a REST API? Integration with IoT Core MQTT is possible anyway. Remember I mentioned above about wanting to use a non-GraphQL version of the service to handle WebSockets, and this is the service I was referring to.
Architecture
Everything related to IoT is not my area of expertise. But that didn't stop me from exploring.
I believe Amazon IoT Core MQTT can be used for real-time front-end feature switching . With the help of the Amazon Amplify plugin , the process of integrating the frontend with the service should be straightforward. Here's how I used Amazon IoT Core to build a real-time front-end feature switch .
Summarize
Developers have plenty of options when it comes to serverless WebSockets on Amazon's cloud technology platform: from GraphQL-related services to the eye-catching usage of the Amazon IoT Core MQTT topic, it's very rich.
I hope this article can help you gain something. I plan to provide more basic information about each service, but also encourage you to explore on your own.
If you have any questions, feel free to send me a private message via Twitter: @wm_matuszewski .
Thanks for reading.
Article by Wojciech Matuszewski
Wojciech Matuszewski for AWS Community Builders
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。