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

How to mix spring-data-rest with spring websocket into a single 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.


The same architecture has bugged my mind for a while now and it will be a long story if I want to mention all the drawbacks and advantages of it so let me jump into the implementation.

The second scenario is valid, but as you mentioned its better to perform the crud actions on same websocket session. This shall remove the need for HTTP handshakes on every request, and reduces the body size of messages, therefore you will have better latency. Meanwhile, you already have a persisting connection to a server, so why not make good use out of it?

I searched around for a while and after 6 years from your question, I still couldn't find any websocket protocols that can make this happen, so I decided to work on that by myself cause I needed it for another dummy project.

Another advantage of such protocol could be that it doesn't require much changes to your already written controllers. So it should be able to support Spring Framework (for example) annotations and make websocket endpoints out of it.The hard part about implementing such protocol in another framework like spring is that as its not nice to create ServletRequest and ServletResponse and convert them to your own websocket protocol, you loose some advantages. For example, any http filter you have written in your application till that moment, will be meaningless because its not really easy to pass your websocket messages through those filters.

About the protocol itself: I decided everything to be passed through json format, alongside a unique id for each request so we can map callbacks on client side to the request id. And of course there is a filter chain to add your filters to it.

Another hard to deal thing here is Spring Security as that too works like http filters in some cases. In my own lib I could finally handle annotations like @PreAuthorize but if you are using antMatchers in your HTTP Security Config, it would be a problem.

Therefore, creating websocket adapter to call http controllers will have many drawbacks.

You can check out the project here: Rest Over Websocket. Its written for Spring Boot.