I'd like to synchronize the state to all the clients interested in particular entity changes. So I'd like to achieve something like:
HTTP/REST
and websockets
)websockets
topicSo technically, I'd be interested in ideas to mix spring-data-rest with spring websockets implementation to achieve something like spring-data-websocket.
There are a two solutions coming to my mind, and in fact both would be:
REST/HTTP API
websocket
controllers (used for the modification calls on entities) The 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:
REST/HTTP API
REST/HTTP API
for all the operationswebsocket
message comes as wellTechnically, could be achieved, by:
websocket
controllers from the spring-rest-data events (namely in the AfterCreateEvent
, AfterSaveEvent
, AfterLinkSaveEvent
, AfterDeleteEvent
)Still the solution seems quite sick to me, as I'd need to go for:
HTTP
request--> Server (spring-data-rest controller)websocket
message--> Spring websocket
controllerwebsocket
message via topic--> all Clients interested in the topicHTTP
response--> client AScenario 2: Websocket API
independent from REST API
Rules:
REST/HTTP API
for non-modifying operations onlyREST/HTTP API
for non-modifying operations onlywebsocket
message for all the modifying operationswebsocket
message is sent to client for all the modifying operations onlyWell, 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 only the routes in my implementation.
As I feel like I'd have to manually expose (via *WebSocketController
s) all the CUD
methods for all my entities. And I might be too lazy for that.
Ideas?
REST requires a stateless protocol according to the statelessness constraint, websockets is a stateful protocol, so it is not possible.
In order to tell Spring to forward client requests to the endpoint , we need to register the handler. Start the application- Go to http://localhost:8080 Click on start new chat it opens the WebSocket connection. Type text in the textbox and click send. On clicking end chat, the WebSocket connection will be closed.
If you hit the link with your browser, you will probably get an error Can "Upgrade" only to "WebSocket" . This is because browsers not open WebSockets by default, this needs a proper client. Since we not yet implemented a real client it is hard to verify our implementation.
Scenario 2 talks about, in the last step, a single client.But I thought your requirement was for a topic since you wanted multiple clients. If I wanted to complete 2 for your stated requirement, then you might want to maintain a list of clients and implement your own queue or use a ForkJoinPool to message all your clients listening in on your WebSockets. Having said that, A topic is definitely more elegant here but overall looks too complicated with different interfaces
For all messages from client to server, just go with a simple wire protocol and use a collection to parameterize, it could be RParam1.......
At the server, you need a controller to map these to different requests(and operations). Somehow does not look like too much work. Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With