Custom assignment with RabbitMQ

I am trying to use RabbitMQ as a broker in my project and I want to assign a destination queue when a socket is opened on the client side. Something like this: http://i.imgur.com/ldB2M0m.png

I managed to do it with help SimpleBroker

, however, when I try to do it with help StompBrokerRelay

, I cannot define the queue on RabbitMQ and stop receiving messages on the client ( http://i.imgur.com/gNaRHCQ.png ).

This is how I do it:

Controller:

@RestController
public class FeedController {

@Autowired
private SimpMessageSendingOperations template;

    @RequestMapping(value = "/feed",  method = RequestMethod.POST, consumes = "application/json")
    public Reference getLeankrReference(@RequestBody Reference ref)
    {       
        this.template.convertAndSendToUser(ref.getChannelId(), "/topic/feed", ref);
        return ref;
    }
}

      

Websocket config:

@Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config)
    {
        config.enableStompBrokerRelay("/topic/")
            .setAutoStartup(true);

        //config.enableSimpleBroker("/user/");
        config.setApplicationDestinationPrefixes("/app");
    }

    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/vision").withSockJS();
    }
}

      

Customer:

        function connect() {
        var socket = new SockJS('/ws/vision');
        var channel = document.getElementById('name').value;
        stompClient = Stomp.over(socket);

        stompClient.connect({}, function(frame) {
            setConnected(true);
            console.log('Connected: ' + frame);
            stompClient.subscribe('/user/' + channel + '/feed', function(message) {
                showContent(JSON.parse(message.body));
            });
        });
    }

      

I know I am missing something. Maybe some kind of brokerage config?

Thank you in advance!

+3


source to share


1 answer


Finally I figured out what I was missing!

Websocket config:

I only assigned a queue of topics. In this case, I also need a queue queue as soon as I want to assign it to a specific user / channel.

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

      

Customer:

I didn't mean the type of queue I wanted to use.

stompClient.subscribe('/user/queue/feed', function(content) {



But that was not enough. Lack of correct security configuration.

Something like that,

Security configuration:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf().disable()
        .headers().addHeaderWriter(
            new XFrameOptionsHeaderWriter(
                    XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN)).and()
        .formLogin()
            .defaultSuccessUrl("/index.html")
            .loginPage("/login.html")
            .failureUrl("/login.html?error")
            .permitAll()
            .and()
        .logout()
            .logoutSuccessUrl("/login.html?logout")
            .logoutUrl("/logout.html")
            .permitAll()
            .and()
        .authorizeRequests()
            .antMatchers("/**").permitAll()
            .anyRequest().authenticated()
            .and();
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
        .inMemoryAuthentication()
            .withUser("channel1").password("password").roles("USER");
}

      

With that, I added a login page. It's not obligatory. You just need to make sure the password parameter is used for authentication.

Now that Rabbit knows the user / channel, he can send queues to specific destinations.

0


source







All Articles