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

Websockets support?

Open mechite opened this issue 2 years ago • 2 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

Could we see some kind of websocket support here

Yes. The plan is to add support for Websockets. We haven't done it to date because A) haven't hit the need personally plus B) Nima Websockets support is still WIP and it's good to have 2 implementations to target.

My thought was to also support SSE (Server Sent Events) but I need to look at the details there. Javalin has SSE but I need to check that in Helidon.

rbygrave avatar Jan 30 '23 19:01 rbygrave

Can we revisit? Has Helidon added enough features to support this? I see a lot of SSE and Websocket features now available.

re-thc avatar Sep 22 '24 08:09 re-thc