2

My goal is to subscribe a client to a server events SSE. Clients will use WebSocket protocol, thus I use: spring.rsocket.server.transport: websocket

The analog on blocking IO stack:

@Configuration
@EnableWebSocketMessageBroker
class WebSocketConfiguration: WebSocketMessageBrokerConfigurer {

    override fun configureMessageBroker(registry: MessageBrokerRegistry) {
        registry.enableSimpleBroker("/topic")

        registry.setApplicationDestinationPrefixes("/app")
    }

    override fun registerStompEndpoints(registry: StompEndpointRegistry) {
        registry.addEndpoint("/websocket")
                .setAllowedOrigins("*")
                .withSockJS();
    }
}

Usage:

@Service
class MessagingService(private val simpMessagingTemplate: SimpMessagingTemplate) {

    private val logger = KotlinLogging.logger {}

    fun notify(baseEvent: BaseEvent) {
        logger.debug { "Sending an event $baseEvent" }
        simpMessagingTemplate.convertAndSend("/topic/events", baseEvent)
    }
}

Client code:

function connect() {
    var socket = new SockJS('/websocket');
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function (frame) {
        setConnected(true);
        console.log('Connected: ' + frame);
        stompClient.subscribe('/topic/events', function (event) {
            console.log('Handled message from ws: ' + event)
            showGreeting(JSON.parse(event.body));
        });
    });
}

Where simpMessagingTemplate pushes an event to connected clients.

I want to achieve the same with reactor and spring-boot-starter-rsocket which works with WebSocket also at client-server level.

Rsocket request-stream fits but in this case, I need eternal flux stream on server cause I don't know when an event will come. I don't know how to do it

Artem Ptushkin
  • 1,151
  • 9
  • 17
  • 1
    Please see this post https://stackoverflow.com/questions/61045066/send-message-only-to-certain-client-using-websockets-with-rsocket-and-spring-web/61452534#61452534 I think it should solve your problem. – kojot Apr 27 '20 at 06:16
  • I'll try, ty. I keep the post open and gonna leave a solution later. It could be different than at that question – Artem Ptushkin Apr 27 '20 at 09:11

0 Answers0