Matthew
Matthew

Reputation: 13

How to Set Up WebSocket Support in Dropwizard 4 with Jetty 11?

I'm currently working on a Dropwizard 4 project with Jetty 11, and I'm having trouble setting up WebSocket support. Previous recommendations recommend using https://github.com/LivePersonInc/dropwizard-websockets but that seems to be outdated. All recommendations that I have found use a class JettyWebSocketServletContainerInitializer which I can't find in any of the dependencies that I have. I am using dropwizard guicy, but I don't think that has any effect here.

Current Dependencies:

<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-server</artifactId>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-servlet</artifactId>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty.websocket</groupId>
    <artifactId>websocket-jakarta-server</artifactId>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty.websocket</groupId>
    <artifactId>websocket-jetty-server</artifactId>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty.websocket</groupId>
    <artifactId>websocket-jetty-api</artifactId>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty.websocket</groupId>
    <artifactId>websocket-jakarta-common</artifactId>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty.websocket</groupId>
    <artifactId>websocket-core-server</artifactId>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty.websocket</groupId>
    <artifactId>websocket-core-common</artifactId>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty.websocket</groupId>
    <artifactId>websocket-jetty-client</artifactId>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty.websocket</groupId>
    <artifactId>websocket-jetty-common</artifactId>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty.websocket</groupId>
    <artifactId>websocket-servlet</artifactId>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty.websocket</groupId>
    <artifactId>websocket-server</artifactId>
    <version>9.4.56.v20240826</version>
</dependency>
<dependency>
    <groupId>jakarta.websocket</groupId>
    <artifactId>jakarta.websocket-api</artifactId>
    <version>2.1.1</version>
</dependency>

All the ones without versions are using the 11.0.20. I realise I don't need all these dependencies, if you could let me know what I am missing and which ones I need that would be great. I've tried various versions to try and get it to work but I'm clearly missing something.

A small code snippet of the setup in the application would be really helpful.

Upvotes: 0

Views: 63

Answers (1)

Mark
Mark

Reputation: 847

This project helped me out: https://github.com/TomCools/dropwizard-websocket-jsr356-bundle.

If you are using Guice for DI, you'll want to add a Configurator:

  ServerEndpointConfig serverEndpointConfig = ServerEndpointConfig.Builder.create(MyWebsocketServer.class,
                                                                                    "/my-ws-enpoint")
            .configurator(new GuiceConfigurator())
            .build();

class GuiceConfigurator extends ServerEndpointConfig.Configurator {
        @Override
        public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
            return injector.getInstance(endpointClass);
        }
    }

Upvotes: 0

Related Questions