2

Every session data passed into the socket is broadcasted to all users since every session subscribes to the UnicastProcessor eventPublisher.

How can I send by event data to a single session id and not to all of them?

@Override
public Mono<Void> handle(WebSocketSession session) {
    WebSocketMessageSubscriber subscriber = new WebSocketMessageSubscriber(eventPublisher);
    session.receive()
            .map(WebSocketMessage::getPayloadAsText)
            .map(this::toEvent)
            .subscribe(subscriber::onNext, subscriber::onError, subscriber::onComplete);
    return session.send(outputEvents.map(session::textMessage));
}

My use-case requires me to include both options for broadcasting any changed state with any client to all sockets connected plus the abillity to send response to a specific client (sessionId) that send a request within a specific event

Github link

or should It be routed to 2 different handlers from the same websocket path? note that from javascript

new WebSocket(url/path)  creates a socket connection

there is no way to change the path without creating or instantiating a new WebSocket object which is not wanted. I'm not interested in creating for every browser client 2 sockets... so my goal is to base the server connection via 1 single websocket path

@Bean
public HandlerMapping webSocketMapping(UnicastProcessor<Event> eventPublisher, Flux<Event> events) {
    Map<String, Object> map = new HashMap<>();
    map.put("/websocket/chat", new ChatSocketHandler(eventPublisher, events));
    SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
    simpleUrlHandlerMapping.setUrlMap(map);

    //Without the order things break :-/
    simpleUrlHandlerMapping.setOrder(10);
    return simpleUrlHandlerMapping;
}

if so would be glad to see an example of such solution

USS-Montana
  • 397
  • 4
  • 15
  • 1
    A tip for making your question answerable: we can't read an entire github project. Find the part in that project where the broadcast is made, and how it's connected to the project, then paste it here with some example. Then I bet we can help you easly ;) – Leviand Jul 31 '18 at 08:36
  • thanks for the tip, updated the post with speific .java file and line number. thumbs up :) – USS-Montana Jul 31 '18 at 08:46

1 Answers1

0

With servlet based web socket, it's possible because you can connect websocket to messaging brokers. Then the messaging broker will take care of sending messages to specific client. But with webflux based websocket that spring is provided , I couldn't manage to do bring messaging brokers into action. It seems that there is no support yet for it in spring webflux.

Find a sample with servlet stack here: https://github.com/bmd007/RealtimeNoteSharing.git

Mahdi Amini
  • 402
  • 1
  • 3
  • 17