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.
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);
}
}
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
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.
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)));
}
When the user clicks the ‘Send’ button, the client uses the webstomp.send method to send a message to the destination (with empty headers). Java Spring client consists of two parts: Spring STOMP events handler and Spring STOMP over WebSocket configuration. To handle STOMP session events, the client implements the StompSessionHandler interface.
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 stomp.js library whilst receiving.
Among STOPM, there are officially registered several messaging subprotocols that work over WebSocket, among them: AMQP (Advanced Message Queuing Protocol) — another protocol to communicate between clients and message brokers MSRP (Message Session Relay Protocol) — a protocol for transmitting a series of related instant messages during a session
WebSocket is a protocol that allows simultaneous bi-directional transmission of text and binary messages between clients (mostly browsers) and servers over a single TCP connection. WebSocket can communicate over TCP on port 80 (“ws” scheme) or over TLS/TCP on port 443 (“wss” scheme).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With