事件处理在Spring Boot和Reactjs之间的处理

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

Events handling between Spring boot and Reactjs

问题

这可能看起来像是一个在互联网上找到的简单解决方案,但请相信我,我已经看过很多例子,却无法确定选择哪种方法。

需求
在应用服务端(spring boot/Java)有一个订阅者,订阅了区块链事件(corda)。我希望在状态发生变化时将此事件推送到 UI(ReactJS)。

我已经成功订阅了区块链事件,但在将其推送到 UI 并让 UI 接收我的事件方面遇到了多种不完整或混乱的想法(请不要建议付费服务、API、库等)。

我已经尝试过并了解了所有的方法,因为我是在处理事件方面的新手,我需要一些指引,了解如何朝着一个完整的解决方案前进。

  1. 发布-订阅模式
  2. 观察者模式
  3. Sse 发射器
  4. Flux 和 Mono
  5. Firebase(明确的拒绝)

+疑惑

  1. 在服务和 UI 之间处理事件,是通过 API/终端调用还是可以直接发射?根据事件名称,我们可以在 UI 中订阅它吗?
  2. 我是否应该有两个专门用于此目的的 API?一个用于触发订阅,另一个用于实际执行发射?
  3. 如果端点始终在侦听,那么它是否需要专用资源?

基本上,我需要一个明确的方法来处理这个问题。

根据需求可以提供代码

英文:

This might seem like an easy solution got on the internet, but believe me, I have seen through a lot of examples and couldn't figure out which approach to choose.

Requirement :
I have a subscriber at the application service(spring boot/Java) end, subscribed to blockchain events( corda ). I want to push this event to UI (ReactJS) whenever there is a change in state.

I could subscribe to the blockchain events successfully but stuck with multiple in-complete or tangled ideas of pushing it to the UI and how UI would receive my events ( kindly don't suggest paid services, APIs, Libraries etc ).

I have come across and tried out all approach, since I'm newly working on events I need some ray of light as to how to approach towards a complete solution.

  1. Publisher-subscriber pattern
  2. Observable pattern
  3. Sse emitter
  4. Flux & Mono
  5. Firebase ( a clear NO )

+Boggler :

  1. events handling between service and UI , should it be via API/endpoint calls or can it be emitted just in air( i'm not clear) and based on event name can we subscribe to it in UI ?
  2. should i have two APIs dedicated for this ? one trigger subscribe and other actually executes emitter ?
  3. If the endpoint is always being heard doesn't it needs dedicated resource ?

I basically need a CLEAR approach to handle this.

Code can be provided based on demand

答案1

得分: 1

我看到你提到你能够在Spring Boot中捕获事件。因此,你只需将事件信息发送到前端。我可以想到三种方法来实现这个:

  • Websockets:可能有些过于复杂,因为我猜想你不需要双向通信。
  • SSE:可能比WebSockets更好的选择。
  • 或者简单的轮询:如果你不需要实时通知,这也是一个不错的选择。
英文:

I see you mention you are able to capture events in Spring Boot. So you are left with sending the event information to the front-end. I could think of three ways to do this.

  • Websockets: Might be an over-kill, as I suppose you won't need bi-directional communication.
  • SEE: Perhaps a better choice than WebSockets.
  • Or simply Polling: Not a bad choice either, if you are not looking for realtime notifications.

答案2

得分: 1

是的,长轮询。

解决方案似乎非常简单。只需建立一次连接,让它们等待尽可能长的时间。这样,在此期间如果有任何新数据到达服务器,服务器就可以直接返回响应。这种方式肯定可以减少涉及的请求数量和响应周期。

您将在互联网上找到关于如何在Spring Boot项目中实现长轮询的多个示例。

英文:

Yes Long Polling.

The solution seems to be pretty simple. Make the connection once and let them wait for as long as possible. So that in the meanwhile if any new data comes to the server, the server can directly give the response back. This way we can definitely reduce the number of requests and response cycles involved.

You will find multiple implementation examples of How Long Polling is done as part of Spring Boot project on internet.

huangapple
  • 本文由 发表于 2020年9月29日 20:09:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64119322.html
匿名

发表评论

匿名网友

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

确定