如何从Spring Kafka后端在Angular前端接收实时通知

huangapple go评论62阅读模式
英文:

How to receive real time notifications in Angular frontend from Spring kafka backend

问题

我在思考是否可以从Spring Kafka后端向Angular客户端发送实时用户通知,我的方法是通过Kafka主题发送通知,而不是使用WebSocket STOMP,并为每个用户创建一个主题,在某种方式上,Angular订阅当前登录用户的主题并监听他们的通知。我试图寻找一个适用于Angular客户端的Kafka客户端库,但我没有找到有用的内容,我找到的最好的是一个已经有10年历史的库,老实说,我不太相信一个过时的库能胜任这项工作。谢谢。

英文:

I am wondering if it is possible to send real time user notifications to an angular client from spring kafka backend, my approach was to send it through the kafka topics instead of using websocket STOMP, and create a topic for each user, and in someway the angular subscribes to the current logged in user topic and listen to their notifications, I tried to look for a kafka client library for the angular client but I didn't find anything useful, my best shot was a library that is 10 years old and I honestly don't trust an out of date library to do the job.
Thank you

答案1

得分: 1

Kafka不是前端技术,因此不存在客户端JS库,并且不会推送事件。

你可以使你的Kafka消费者应用程序做任何你想做的事情,比如发送GCM/APNS消息,或将数据转发到负责发送此类通知的其他系统。但是,“每个用户一个主题”通常是Kafka中的反模式,因为代理服务器对它们支持的主题数量有限制,而你的用户群可能无限增长。

英文:

Kafka isn't a frontend technology such that a client side JS library exists, and doesn't push events.

You can make your kafka consumer application do anything you want, such as send GCM/APNS messages, or forward data to some other system that is responsible for sending such notifications. But "one topic per user" is generally an anti pattern with Kafka since the brokers do have a limit on number of topics they support, but your userbase might grow indefinitely.

答案2

得分: 0

我们使用服务器推送事件(Server-Sent Events,SSE)实现前端和后端之间的单向实时通信。 SSE允许后端向前端发送更新或通知,而无需前端显式请求。这对我们提供实时通知给用户的需求非常合适。

另一方面,Kafka用于系统内部微服务之间的实时通信。它充当消息代理,实现不同微服务之间的消息和更新交换。在我们的架构中,通知微服务负责根据通过Kafka从其他微服务接收到的事件或消息发送通知。

总体而言,SSE促进了前端和后端之间的实时通信,而Kafka促进了微服务之间的实时通信和消息传递。通过同时使用SSE和Kafka,我们可以确保在系统的前后端和微服务层面上实现高效可靠的实时通信。

英文:

We used Server-Sent Events (SSE) for one-way real-time communication between the frontend and backend. SSE allows the backend to send updates or notifications to the frontend without the need for the frontend to explicitly request them. This was suitable for our requirements in providing real-time notifications to the users.

On the other hand, Kafka is employed for real-time communication between microservices within the system. It serves as a message broker, enabling the exchange of messages and updates between different microservices. In our architecture, the Notification microservice is responsible for handling the sending of notifications based on the events or messages received from other microservices via Kafka.

In summary, SSE facilitates real-time communication between the frontend and backend, while Kafka facilitates real-time communication and messaging between microservices. By utilizing both SSE and Kafka, we can ensure efficient and reliable real-time communication at both the frontend-backend and microservice levels in our system.

huangapple
  • 本文由 发表于 2023年5月21日 05:28:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297420.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定