0

Currently in my application, a message is broadcast each 10 second with spring websockets. This is how the messages are broadcast to users in my spring application.

    @Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
@Component
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

      @Autowired  
      private SimpMessagingTemplate template;

      private TaskScheduler scheduler = new ConcurrentTaskScheduler();

      public WebSocketConfig() {
            System.out.printf(" ---INIT----------");

    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/simplemessages").withSockJS();
    }

    // @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {

       config.enableSimpleBroker("/topic/", "/queue/");

        config.setApplicationDestinationPrefixes("/app");
    }

    @PostConstruct
    private void broadcastTimePeriodically() {


      scheduler.scheduleAtFixedRate(new Runnable() {
         public void run() {
             try{
             template.convertAndSend("/topic/simplemessagesresponse", "{shares:true,price:100.00}");
             }catch(MessagingException e){
                 System.err.println("!!!!!! websocket timer error :>"+e.toString());
             }

        }
      }, 10000);
    }

   @PreDestroy
   private  void destroyServices(){

   }

   // @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {

    }
  // @Override
   public void configureClientOutboundChannel(ChannelRegistration registration) {
      registration.taskExecutor().corePoolSize(4).maxPoolSize(10);
  }

    //@Override
    public boolean configureMessageConverters(List<MessageConverter> arg0) {
        // TODO Auto-generated method stub
        return true;
    }
    @Override
    public void configureWebSocketTransport(WebSocketTransportRegistration arg0) {
        // TODO Auto-generated method stub

    }
}

This is how the browser receives,

var socket = new SockJS(desz);
stompClient = Stomp.over(socket);

stompClient.connect('user', 'guest', function(frame) {

   stompClient.subscribe("/topic/simplemessagesresponse", function(servermessage) {
        var stompResponse =  JSON.parse((servermessage.body));
        console.log('server msg: '+stompResponse);
    });
});

I want to broadcast same message to some users, while another set of users have another message periodically. How I should modify my above code to achieve this ?

Débora
  • 5,816
  • 28
  • 99
  • 171

1 Answers1

0

You can have this in your scheduler run() method

this.simpMessagingTemplate.convertAndSend("/queue/" + userGroup.geName(),
            messageMap.get(userGroup.geName()));

and in the client side you can subscribe to specific url "queue/{groupName}"

stompClient.subscribe("/queue/${groupName}", function(servermessage) {
    var stompResponse =  JSON.parse((servermessage.body));
    console.log('server msg: '+stompResponse);
});

NOTE :(in client example variable 'groupName' is sent to view from controller and accessed using EL in JSP)

Jebil
  • 1,144
  • 13
  • 25