stompjs icon indicating copy to clipboard operation
stompjs copied to clipboard

Sending binarybody to activeMQ doesn't work

Open jonim8or opened this issue 2 years ago • 1 comments

I already checked the FAQ's, and #113 and #22. This could well be an AmQ bug, but I wanted to mention it here as I'm not 100% sure.

Use case: I have a nodejs typescript project, and want to send messages over activeMq classic (not activeMQ artemis), and based on their size, either send them in plain text or zipped. Using a different activeMq or a different protocol/library would be much more effort, so I want to avoid that.

Test to reproduce the problem: I subscribe to a topic, and send two messages to the topic: one binary and one with plain text. The plain text message is received, the binary is not.

import {Client, Versions} from "@stomp/stompjs";
// import ws from 'ws'
// (global as any).WebSocket = ws

import websocket from 'websocket'
(global as any).WebSocket = websocket.w3cwebsocket

const TOPIC_NAME='/topic/sometopic'
const MSG_BODY='Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'

async function connect(stompUrl:string){
    const stompClient = new Client({brokerURL: stompUrl, logRawCommunication:true})
    stompClient.stompVersions = new Versions(['1.0', '1.1'])
    stompClient.onStompError = (err) => console.error('Stomp error: ' + err.body)
    stompClient.onWebSocketError = (err) => console.error('Ws error: ' + JSON.stringify(err))
    stompClient.debug = function(str) {
        console.log(str);
    };
    await new Promise<void>((res)=>{
        stompClient.onConnect = ()=>res()
        stompClient.activate()
    });
    return stompClient
}


async function main(stompUrl:string) {
    const client = await connect(stompUrl)

    client.subscribe(TOPIC_NAME, message =>{
        //do nothing
    })
    console.debug('===sending binary')
    const binaryBody = Uint8Array.from([0,1,2,3,4,5]);
    client.publish({
        binaryBody,
        destination: TOPIC_NAME,
        skipContentLengthHeader:false,
        headers: {
        }
    })
    console.debug('===completed sending binary')
    console.debug('===sending plain text')
    client.publish({
        body:MSG_BODY,
        destination: TOPIC_NAME,
        skipContentLengthHeader:true,
        headers: {
        }
    })
    console.debug('===completed sending plain text')

    setTimeout(()=>client.deactivate(), 100000)
}

const amqUrl = process.argv.length > 2 ? process.argv[2] : 'ws://host.docker.internal:61614';
main(amqUrl);

The logs suggest that the messages are sent, but activemq's control panel suggests only the text message is sent. image

===sending binary
>>> [object ArrayBuffer]
===completed sending binary
===sending plain text
>>> SEND
destination:/topic/sometopic

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
===completed sending plain text
Received data
<<< MESSAGE
expires:0
destination:/topic/sometopic
subscription:sub-0
priority:4
message-id:ID\c20850fab9d0f-33045-1662536193712-3\c40\c-1\c1\c1
timestamp:1662545849973

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Received data
<<<

I've also ensured that the receiving side is working. If I send a message using a python script (which btw uses stomp over tcp, not ws), the message is received.

What is tested

  • multiple websocket implementations (ws and websocket)
  • multiple Amq versions: 5.16.3, 5.17.2
  • different stomp protocol versions (1.0, 1.1, 1.2) zip.zip

jonim8or avatar Sep 07 '22 10:09 jonim8or

Your code on the JS side looks alright.

The test suits contain sending and receiving binary messages: https://github.com/stomp-js/stompjs/blob/develop/spec/unit/message.spec.js#L70

The tests are executed against RabbitMQ. I do not have any experience with message brokers other than RabbitMQ. Sometimes the broker (for example RabbitMQ) needs additional configuration to enable binary messages over STOMP. Other than that, at this point, I am unable to think of anything else.

kum-deepak avatar Sep 08 '22 03:09 kum-deepak