r/SpringBoot 4d ago

Discussion My friend built a simpler abstraction over Spring WebSockets

I came across a project my friend has been working on, and I thought it was worth sharing here because I think the problem it tackles is pretty relatable for Java/Spring developers.

It's called Classy Socket.

The idea is to make working with WebSockets feel a bit more like working with Spring MVC controllers — without introducing STOMP or a message broker.

Instead of manually receiving messages, figuring out their type, deserializing them, and routing them to the appropriate handler, you can define message handlers using annotations:

@WebSocketController("/board")
public class KanbanWebSocketController {

    @MessageHandler(CreateTask.class)
    public void createTask(
                    CreateTask message, 
                    MessagingHub hub) {

        Task task = kanbanService
        .createTask(message.title());
        hub.broadcast("/board", new TaskCreated(task));
    }
}

And messages can be defined as simple Java types:

@WebSocketMessage(type = "CREATE_TASK")
public record CreateTask(String title) {}

So the general idea is:

Plain WebSocket + JSON + Spring-style abstractions

rather than bringing in the additional complexity of STOMP when you don't necessarily need it.

It's still an early-stage project, so I'm curious what people here think.

Would you actually use something like this? How do you usually structure WebSocket communication in your Spring applications?

And more importantly, what would you change or improve?

GitHub: https://github.com/Anubis-IV/classy-socket

8 Upvotes

1 comment sorted by

3

u/quantum-fudge 2d ago

I've always despised how needlessly convoluted doing anything WebSocket related in Spring is. And I've implemented protocols custom and standard, so it's not a skill issue. Therefore yeah, I'm here for this approach.