8

How to subscribe to AWS Event Bus events from client side applications ex: NodeJS app, Angular client or a Mobile client app ?

In December 2020, an email from AWS Marketing has presented the advantages to use the Event-Driven architecture . Following the documentation and the tutorials, soon I stumble into the wall of not finding a way to subscribe to this events from a client side application.

The email states:

4 Reasons to Care About Event-Driven Architectures

Are you looking to scale and build robust applications without delays and dependencies? We break down the basics of event-driven architectures, how they work, and show you ways to get started. Learn how event-driven architectures can help you:

  • Scale and fail independently - No more dependencies
  • Develop with agility - No custom polling code
  • Audit with ease - Use your event router to define policies
  • Cut costs - Stop paying for continuous polling

The disappointing part is that there is no example of libraries to be integrated in the client side code to subscribe to those event. Googling does not return any significant result and the only current library for node: @aws-sdk/client-eventbridge-node only expose a send and destroy methods.

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
MFAL
  • 1,090
  • 13
  • 19

3 Answers3

9

There is no way to directly subscribe to an Amazon EventBridge bus, as it doesn't provide publish/subscribe functionality. In order to process events in EventBridge you create event rules that filter and send matching events to targets. You can find all targets available to EventBridge rules on this list: Amazon EventBridge Targets.

One of these targets could be an Amazon SNS topic, which provides pub/sub functionality, i.e. your client application can subscribe to the topic to automatically receive the respective events.

This may sound complicated at first, but the implementation is strictly following the principle of separating concerns. It provides simple building blocks--like Lego pieces--that you can put together in order to create truly loosely coupled architectures.

This diagram shows the functionality in scope of Amazon Event Bridge and how it communicates with other services and applications.

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
  • Dennis, thanks for the answer. Then, saying in the marketing message, "no custom polling code" sounds misleading. Even with SNS I cannot subscribe directly, since the SNS HTTP/HTTPS topic creation requires a web-server URL to register and validate. Then the option for a client that does not want to do any polling is again web-sockets using an intermediate web-server or AWS api-gateway (web-socket). – MFAL Dec 25 '20 at 16:54
  • "No custom polling code" means that in many scenarios you don´t need a running application polling for events because you can have events trigger application logic on demand, e.g. a Lambda function. This benefit may not apply to your specific use case as you have a different architecture. As of now there is no way to trigger and run Lambda functions inside the mobile client. However, I´m not an expert in mobile applications and there may be a different option that I´m not aware of. The answer to your original question is unchanged, though: You can´t directly subscribe to en EventBridge bus. – Dennis Traub Dec 25 '20 at 17:04
  • 1
    Oh, I just remembered that there's a service called AWS AppSync. It might be exactly the piece of the puzzle that you've been looking for as it's a fully managed service that makes it easy to create websocket-based subscriptions that support real-time updates and integrate them with other AWS services: https://aws.amazon.com/appsync You can find the real-time reference architecture here: https://aws.amazon.com/blogs/mobile/aws-appsync-real-time-reference-architecture/ – Dennis Traub Dec 25 '20 at 17:17
  • Further to this, if you're not using a serverless solution like Lambda, you may wish to proxy eventbridge -> SQS and poll from SQS. The benefit in my experience if you have something like an ECS service which polls and processes the messages, you will be able to scale that based on the size of the SQS Queue without your service being overwhelmed. When I've used SNS in the past the service hasn't scaled quickly enough and web servers can get funky and crash if too many requests stack up. – Alex Bailey Dec 27 '20 at 16:03
  • @DennisTraub . Yes, I think that GraphQL/AppSync sounds like the best candidate. – MFAL Aug 04 '21 at 08:35
2

Services that allow you to subscribe as you want it (directly delivering subscribed messages to your code over a tcp connection such as a websocket) are:

  • AppSync - websocket
  • IOT Core - websocket, mqtt
  • SQS - long poll
  • Kafka

(From the top of my head)

So a straightforward serverless solution for you could be:

Event bridge —> SQS —> your code

I often use AppSync for this purpose. But eventbridge is cool too.

Otto
  • 1,787
  • 1
  • 17
  • 25
  • 2
    I wonder why in a lot of AWS documents mention EventBridge as a pub/sub tool but it is not really pub/sub in traditional sense as the message receivers do not necessarily know about the broker. – reza May 03 '21 at 21:22
1

if you want to avoid polling and you cant/want use AWS Lambda then you can reverse the problem and call an api on your application from EventBridge with a rule.

You can create API Targets in EventBridge:

API destination

giammin
  • 18,620
  • 8
  • 71
  • 89