avaje-http icon indicating copy to clipboard operation
avaje-http copied to clipboard

Websockets support?

Open Mechite opened this issue 1 year ago • 1 comments

Could we see some kind of websocket support here or in a seperate avaje-sockets library? It would be nice to be able to have a Spring-like system for them too, in a glimpse I see this guide from Spring with the following sample:

(...)
@Controller
public class GreetingController {

  @MessageMapping("/hello")
  @SendTo("/topic/greetings")
  public Greeting greeting(HelloMessage message) throws Exception {
    Thread.sleep(1000); // simulated delay
    return new Greeting("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!");
  }
}

This controller is concise and simple, but plenty is going on. We break it down step by step.
The @MessageMapping annotation ensures that, if a message is sent to the /hello destination, the greeting() method is called.
The payload of the message is bound to a HelloMessage object, which is passed into greeting().
Internally, the implementation of the method simulates a processing delay by causing the thread to sleep for one second. This is to demonstrate that, after the client sends a message, the server can take as long as it needs to asynchronously process the message. The client can continue with whatever work it needs to do without waiting for the response.
After the one-second delay, the greeting() method creates a Greeting object and returns it. The return value is broadcast to all subscribers of /topic/greetings, as specified in the @SendTo annotation. Note that the name from the input message is sanitized, since, in this case, it will be echoed back and re-rendered in the browser DOM on the client side.

This is a pretty beautiful system, but obviously as all things Spring is bloated and obnoxious, and Javalin (and Heildon actually, I've personally zero experience with Heildon so I'm probably stating things you're already aware of) already provides support for websockets. Javalin has websocket registration right inside of ApiBuilder and I imagine Heildon has a simple system to do so as well, so it shouldn't be too much work to get this to work.

Mechite avatar Jan 22 '23 19:01 Mechite