Search code examples
jerseymuleserver-sent-eventshttplistener

How to implement SSE in Mule 3.9.0 using http listener and jersey


Is it possible to implement server sent events (SSE) with mule (3.9.0) and jersey? It seems that the mule http listener immediately closes the connection. Is there a way for mule to keep the socket open?

I have implemented the configuration:

<http:listener-config name="HttpListenerConfig" host="0.0.0.0" basePath="/myservice" port="8090" />

<flow name="NotificationServiceFlow">
        <http:listener config-ref="HttpListenerConfig" path="/sse/*" allowedMethods="GET" parseRequest="false" responseStreamingMode="ALWAYS">
        </http:listener>
        <jersey:resources>
            <component>
                <spring-object bean="NotificationResource"/>
            </component>
        </jersey:resources>
    </flow>

and the jersey resource as follows:

@Path("/notifications")
public class NotificationResource {
    
    private SseBroadcaster broadcaster = new SseBroadcaster();
    
    @GET
    @Produces(SseFeature.SERVER_SENT_EVENTS)
    public EventOutput subscribe() {
        final EventOutput eventOutput = new EventOutput();
        broadcaster.add(eventOutput);
        return eventOutput;
    }
    
    public void broadcast(String json) {
        OutboundEvent.Builder builder = new OutboundEvent.Builder();
        OutboundEvent event = builder
            .mediaType(MediaType.APPLICATION_JSON_TYPE)
            .data(String.class, json)
            .build();
        broadcaster.broadcast(event);
    }
}

Solution

  • Server side events are not supported in any version of Mule as far as I know.

    An alternative could be to use Mule 4 Websockets connector: https://docs.mulesoft.com/websockets-connector/1.0/

    In any cases using Mule 3.9.0 is unsafe. It is recommended to migrate to the latest release of Mule 4.