15

I am able to send and receive JSON with STOMP over WebSockets following spring documentation. However performance is poor at large high rates, so I wish to profile the use of binary messages.

  • Spring-WebSockets 4.0
  • JavaScript client running in Chrome 35
  • stomp.js 1.7.1

Sending

I send messages using SimpMessageTemplate with the necessary broker relay - see spring documentation

@Controller
public class DemoBinaryController {
   @Autowired
   private SimpMessagingtemplate template

   @Scheduled(fixedDelay = 5000)
   public void demo() throws Exception {
      GenericMessage<byte[]> message = new GenericMessage<byte[]>(new byte[]{65,66,67});
      template.send("/app/binarydemo", message);
   }
}

Receiving

A JavaScript client receives data using stomp.js using the standard mechanism.

var subscription = client.subscribe("/app/binarydemo", new function(message) {
   console.log("RX message", typeof message.body, message.body.length);
});

Messages are received, but as Strings, with console output as follows. I'm expecting some raw type, like ArrayBuffer

RX message string  3
RX message string  3

Things I've tried

I realise the T in STOMP stands for Text, however the Spring documentation implies binary messages are possible at least with plain WebSockets, also the stomp specification states

STOMP is text based but also allows for the transmission of binary messages.

  • Debugging the sending code, and it appears to remain as byte [] for as far as I can see
  • Debugging the stomp.js library whilst receiving. The message appears to be a String when received in the underlying ws.onmessage callback (line 243 in stomp-1.7.1.js)
  • Lots of searching - this seems a rare topic with little information
  • Looking at the stomp.js source code. The only reference to binary is ws.binaryType = "arraybuffer".

Update: I've done more debugging on the server side. It seems that org.springframework.web.socket.TextMessage is always used within org.springframework.web.socket.messaging.StompSubProtocolHandler rather than org.springframework.web.socket.BinaryMessage. I've raised a feature request for this SPR-12301

message = MessageBuilder.withPayload(message.getPayload()).setHeaders(headers).build();
byte[] bytes = this.stompEncoder.encode((Message<byte[]>) message);

synchronized(session) {
    session.sendMessage(new TextMessage(new String(bytes, UTF8_CHARSET)));
}

My question

  • Is this approach possible with this mix of technologies?
  • Am I missing some crucial step?
  • Can anyone point me to a working example
Adam
  • 35,919
  • 9
  • 100
  • 137

2 Answers2

14

It seems that org.springframework.web.socket.TextMessage is always used within org.springframework.web.socket.messaging.StompSubProtocolHandler rather than org.springframework.web.socket.BinaryMessage. I've raised a feature request for this SPR-12301 which has been accepted.

message = MessageBuilder.withPayload(message.getPayload()).setHeaders(headers).build();
byte[] bytes = this.stompEncoder.encode((Message<byte[]>) message);

synchronized(session) {
    session.sendMessage(new TextMessage(new String(bytes, UTF8_CHARSET)));
}

Update

  • SPR-12301 was delivered in 4.1.2 but only adds support for receiving binary messages
  • Raised SPR-12475 for sending of binary messages
Adam
  • 35,919
  • 9
  • 100
  • 137
  • Do we still have the same above solution to sending binary data using stomp over websocket (without sockjs) ? – UserBSS1 May 06 '20 at 13:18
  • Hi @Adam (+1) -- to your knowledge did server-side binary support ever get added to Spring? Meaning: the server sends binary to HTTP clients? It looks like the GH issues were just closed without any comments/updates. I'm in a similar predicament and was hoping (8 years later!) it would have been addressed by now. Thanks for any and all info! – hotmeatballsoup Oct 20 '22 at 15:03
2

Try to configure you Server just with ByteArrayMessageConverter:

@Configuration
@EnableWebSocketMessageBroker
public class MyWebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
   
   @Override
    public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
        messageConverters.add(new ByteArrayMessageConverter());
        return false;
    }

}

UPDATE

Ah! I see that. Thanks:

public TextMessage(byte[] payload) {
    super(new String(payload, UTF_8));
    this.bytes = payload;
}

From other side from STOMP spec:

The body of a STOMP message must be a String. If you want to send and receive JSON objects, ...

According to the ArrayBuffer:

Getting an array buffer from existing data

  • From a Base64 string
  • From a local file

So, I think you don't have a chioce rather than provide you some custom MessageConverter, which converts your byte[] to Base64 String and send it.

On the JavaScript side you should decode that string to the ArrayBuffer somehow.

Community
  • 1
  • 1
Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • thanks for the suggestion. I've tried that but it does not fix the problem. I've debugged it, the config class is created and configureMessageConverters is called. I've tried putting breaks on ByteArrayMessageConverter.supports, convertFromInternal and convertToInternal, but none of these methods are called, even though it still manages to send to the client! - yes I'm definitely in Debug mode. – Adam Oct 06 '14 at 11:45
  • Added more info to the answer – Artem Bilan Oct 06 '14 at 12:16
  • 1
    From [STOMP spec](https://stomp.github.io/stomp-specification-1.2.html#Protocol_Overview): "STOMP is text based but also allows for the transmission of binary messages." - the header can be followed by binary data, in this case the header must specify a content length. (JSON is not mentioned in the official spec at github) – mjn Oct 20 '14 at 16:48