title: How to send ERROR message to STOMP clients with Spring WebSocket?
body: <p>I am using Spring's STOMP over WebSocket implementation with a full-featured ActiveMQ broker.  When users <code>SUBSCRIBE</code> to a topic, there is some permissions logic that they must pass through before being successfully subscribed.  I am using a ChannelInterceptor to apply the permissions logic, as configured below:</p>
  
  <p><strong>WebSocketConfig.java:</strong></p>
  
  <pre><code>@EnableWebSocketMessageBroker
  public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
  
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
      registry.addEndpoint("/stomp")
        .setAllowedOrigins("*")
        .withSockJS();
    }
  
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
      registry.enableStompBrokerRelay("/topic", "/queue")
        .setRelayHost("relayhost.mydomain.com")
        .setRelayPort(61613);
    }
  
    @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {
      registration.setInterceptors(new MySubscriptionInterceptor());
    }
  
  
  }
  </code></pre>
  
  <p><strong>WebSocketSecurityConfig.java:</strong></p>
  
  <pre><code>public class WebSocketSecurityConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
  
    @Override
    protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
      messages
        .simpSubscribeDestMatchers("/stomp/**").authenticated()
        .simpSubscribeDestMatchers("/user/queue/errors").authenticated()
        .anyMessage().denyAll();
    }
  
  }
  </code></pre>
  
  <p><strong>MySubscriptionInterceptor.java:</strong></p>
  
  <pre><code>public class MySubscriptionInterceptor extends ChannelInterceptorAdapter {
  
    @Override
    public Message&lt;?&gt; preSend(Message&lt;?&gt; message, MessageChannel channel) {
  
      StompHeaderAccessor headerAccessor= StompHeaderAccessor.wrap(message);
      Principal principal = headerAccessor.getUser();
  
      if (StompCommand.SUBSCRIBE.equals(headerAccessor.getCommand())) {
        checkPermissions(principal);
      }
  
      return message;
    }
  
    private void checkPermissions(Principal principal) {
      // apply permissions logic
      // throw Exception permissions not sufficient
    }
  }
  </code></pre>
  
  <p>When clients who do not have adequate permissions attempt to subscribe to a restricted topic, they never actually receive any messages from the topic BUT are also not notified of the exception that was thrown which rejected their subscription.  Instead, the client is handed back a dead subscription that the ActiveMQ broker knows nothing about.  (Normal, adequately-permissioned client interactions with the STOMP endpoint and topics work just as expected.)</p>
  
  <p>I have tried subscribing to <code>users/{subscribingUsername}/queue/errors</code> and just plain <code>users/queue/errors</code> with my Java test client after it is successfully connected, but I have thus far been unable to get sort of error message about the subscription exception from the server delivered to the client.  This is obviously less than ideal since clients are never notified that they've been denied access.</p>