2

I'm new to web socket programming. I have more than 10 methods annotated with @GetMapping, where the returned data is read from a MySQL database.

Can anyone help me to know how to write WebSockets.

My WebRestController.java looks like the below:

@CrossOrigin(origins = "http://localhost:4200", allowedHeaders="*")
@RestController
@RequestMapping("/api")
public class WebRestController {
    @GetMapping("/summary")
    public String Summary() { /* ... */}
    @GetMapping("/erday")
    public String Erday(String erday) { /* ... */}
    @GetMapping("/count")
    public String Count(@RequestParam Map<String,String> queryParam, 
        String date, String target) { /* ... */}
    @GetMapping("/details")
    public String Details(@RequestParam Map<String,String> queryParam, 
        String date, String target) { /* ... */}
    @GetMapping("/devmawah")
    public String DevMawah(@RequestParam Map<String,String> queryParam, 
        String date, String target) { /* ... */}
    // ....
}

I tried before many times in internet to find the solution, but couldn't find it. All I found are examples for Chat applications, which have 2 endpoints to send and receive.

Serban Petrescu
  • 5,127
  • 2
  • 17
  • 34
user9414660
  • 106
  • 1
  • 2
  • 15

3 Answers3

2

WebSockets are used for bi-directional communication, not really for REST style services (where HTTP is superior in my opinion). The core difference being that HTTP is fundamentally a request-reply protocol, which fits very well to REST whereas WebSocket is centred around messages. Of course, you can argue that request-reply is a specialisation of message-based communication.

There are several articles on this topic (google REST over WebSocket) and even some StackOverflow questions which detail the pros and cons, for example Is ReST over websockets possible?.

The only way that I know of which allows you to do something resembling REST over WebSocket without having to re-write the RestController is swagger-socket, but I would not recommend using it as the project seems to be inactive now and it seems to not have been used extensively.

Alternatively, you can replace your @RequestMapping or @GetMapping annotations with @MessageMapping annotations and model your API through messages (e.g. the client sends a "GET" message to a given destination, and you send back a message containing the resources).

Serban Petrescu
  • 5,127
  • 2
  • 17
  • 34
1

I can say 99% of reasons that brings us to the idea of Rest Over Websocket is just because we are looking at the problems from wrong direction.

But I wrote such library because I am going to need it in another dummy project I will write later, and you can check it out.

Basically what it does is to scan your controllers, and create a websocket handler that can pass data to those controllers, depending on path, method, and inputs. But there is a lot more it handles internally to achieve this goal.

I havent started the docs yet, so, you can check the sample from website for now.

Sepehr GH
  • 1,297
  • 1
  • 17
  • 39
0

Well... Just my little contribution... First you you should code your websocket handler (taking into account the potential size the payload). Then, you should create a config class to register your hander (I am thinking not in a stomp version). In your handler, after you stablish the connection with the websocket server, you should have a custom component to hold the sessions (maybe a map of usernames and a session wrapper). This is useful, as you you could send messages not only through the ws connection but also through a rest api endpoint (you could hit an endpoint or using an scheduler tasks to periodically send messages to certain users upon certain conditions). Will post again a link later