How to mix spring-data-rest with spring websocket into a single implementation

admin

Administrator
Staff member
I'd like to synchronize the state to all the clients interested in particular entity changes. So I'd like to achieve something like:

<ul>
<li>exposing CRUD API on entity (via
Code:
HTTP/REST
and
Code:
websockets
)</li>
<li>and routing the response (of the modifying calls) to
Code:
websockets
topic</li>
</ul>

So technically, I'd be interested in ideas to mix <a href="http://projects.spring.io/spring-data-rest/">spring-data-rest</a> with <a href="https://raymondhlee.wordpress.com/2...mplement-two-way-server-client-communication/">spring websockets implementation</a> to achieve something like spring-data-websocket.

There are a two solutions coming to my mind, and in fact both would be:

<ul>
<li>spring-data-rest to expose my entities via
Code:
REST/HTTP API
</li>
<li>
Code:
websocket
controllers (used for the modification calls on entities) </li>
</ul>

The
Code:
websocket
controllers would look like this:

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

<strong>Scenario 1:
Code:
Websocket API
called from
Code:
REST/HTTP API
</strong>

Rules:

<ul>
<li>client request is always
Code:
REST/HTTP API
</li>
<li>response is
Code:
REST/HTTP API
for all the operations</li>
<li>moreover for modifying operations the
Code:
websocket
message comes as well</li>
</ul>

Technically, could be achieved, by:

<ul>
<li>calling the
Code:
websocket
controllers from the <a href="http://docs.spring.io/spring-data/rest/docs/2.0.0.M1/reference/html/events-chapter.html">spring-rest-data events</a> (namely in the
Code:
AfterCreateEvent
,
Code:
AfterSaveEvent
,
Code:
AfterLinkSaveEvent
,
Code:
AfterDeleteEvent
)</li>
</ul>

Still <strong>the solution seems quite sick to me</strong>, as I'd need to go for:

<ol>
<li>client A --
Code:
HTTP
request--> Server (spring-data-rest controller)</li>
<li>Server (AfterXXXEvent in the spring-data-rest controller) --
Code:
websocket
message--> Spring
Code:
websocket
controller</li>
<li>Spring websocket controller --
Code:
websocket
message via topic--> all Clients interested in the topic</li>
<li>Server (spring-data-rest controller) --
Code:
HTTP
response--> client A</li>
</ol>

<strong>Scenario 2:
Code:
Websocket API
independent from
Code:
REST API
</strong>

Rules:

<ul>
<li>client request is
Code:
REST/HTTP API
for non-modifying operations only</li>
<li>response is
Code:
REST/HTTP API
for non-modifying operations only</li>
<li>client sends
Code:
websocket
message for all the modifying operations</li>
<li>
Code:
websocket
message is sent to client for all the modifying operations only</li>
</ul>

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
Code:
C(R)UD
methods exposed via
Code:
websockets
as well, something like spring-data-websockets and handle only the routes in my implementation.

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

Ideas?