spring-projects / spring-projects/spring-data-rest

mixing spring-data-rest with spring websocket implementation [DATAREST-232]

Open
#615 10 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

in: repository type: enhancement
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

Peter Butkovic opened DATAREST-232 and commented

Let me put it in a StackOverflow way (to prevent reformulating my dilemmas to some perfectly designed solution proposal):

I'd like to synchronize the state to all the clients interested in particular entity changes. so I'd like to achieve something like:

  • exposing CRUD API on entity (via HTTP/REST and websockets)
  • and routing the response (of the modifying calls) to websockets topic

So technically, I'd be interested in ideas to mix spring-data-rest with spring websockets implementation to achieve something like spring-data-websocket.

It would be nice if spring-data-rest could support websocket annotations "somehow" :) out of the box. Marking my rest exposed methods via anotations (taking care of websocket message routing would be perfect).

Otherwise, there are a two solutions coming to my mind, in fact in both would be:

  • spring-data-rest to expose my entities via REST/HTTP API
  • websocket controllers (used for the modification calls on entities)

websocket controllers would look like this:

@Controller
public class EntityAWebSocketController {
      @MessageMapping("/EntityA/update")
      @SendTo("/topic/EntityA/update")
      public EntityA update(EntityA entityA) throws Exception {
           // persist,....
           return entityA;
     }
}

Scenario 1: Websocket API called from REST/HTTP API

Rules:

  • client request is always REST/HTTP API
  • response is REST/HTTP API for all the operations
  • moreover for modifying operations the websocket message comes as well

Technically, could be achieved, by:

  • calling the websocket controllers from the spring-rest-data events (namely in the AfterCreateEvent, AfterSaveEvent, AfterLinkSaveEvent, AfterDeleteEvent)

Still it seems quite sick to me, as I'd be going for:

  1. client A ==HTTP request==> Server (spring-data-rest controller)
  2. Server (AfterXXXEvent in the spring-data-rest controller) ==websocket message==> Spring websocket controller
  3. Spring websocket controller ==websocket message via topic==> all Clients interested in the topic
  4. Server (spring-data-rest controller) ==HTTP response==> client A

Scenario 2: Websocket API independent from REST API

Rules:

  • client request is REST/HTTP API for non-modifying operations only
  • response is REST/HTTP API for non-modifying operations only
  • client sends websocket message for all the modifying operations
  • websocket message to client for all the modifying operations only

Well, if no other ideas come up, I'd go for the later one, but still, it would be great if I could have somehow generated C(R)UD methods exposed via websockets as well, something like spring-data-websockets and handle in my implementation only the routes.

As I feel like I'd have to manually expose (via *WebSocketControllers) all the CUD methods for all my entities. And I might be too lazy for that.

Ideas?


Affects: 2.0 M1 (Codd)

Reference URL: https://stackoverflow.com/questions/21278307/mixing-spring-data-rest-with-spring-websocket-implementation

8 votes, 9 watchers

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.