I have ActiveMQ and I’m sending messages to the Topic in backend. And there is Angular app where using Websocket I’m trying to read messages from this topic.
My Websocket creates a connection with ActiveMQ (there is no errors). But I it doesnt read messages from topic.
In ActiveMQ I see
“Number of consumers: 1” |
“Messages Enqueued : 1” |
“Messages Dequeued : 0“
The url for activemq looks like this:
activeMqURL= ‘wss://my-activemq-url.com:61619’
If I go to Active subscribers I cant see the client id name for some reason
Any ideas how to fix it and what is the reason here ?
or may be there is another solution ?
@Injectable({
providedIn: 'root',
})
export class WebsocketService {
registerTopic(topic: string, clientId: string, callback: any) {
let ws: WebSocket;
try {
ws = new WebSocket(`${activeMqURL}`, 'stomp');
ws.onopen = () => {
console.log('Connection open');
ws.send(`CONNECTnclient-id:${clientId}nn`)
ws.send(`SUBSCRIBEndestination:/topic/${topic}nack:autonactivemq.subscriptionName:${clientId}nn`)
};
ws.onmessage = (e) => {
if (e.data.startsWith('MESSAGE')) {
console.log('onmessage ', e.data);
callback(e.data);
}
};
ws.onerror = (error) => {
console.error('WebSocket error:', error);
};
} catch (error) {
console.error('WebSocket connection failed:', error);
}
}
}
it will be used in my component like this
constructor(private websocketService: WebsocketService,
private someService: SomeService) {
this.someService.getCurrentUser()
.subscribe((userId) => {
this.websocketService.registerTopic("order-structure-creation", 'created-client-id', this.callback.bind(this));
}
private callback(data: any) {
let userDTO = JSON.parse(data.substring(data.indexOf("{"), data.indexOf("}") + 1));
if (data) {
console.log('callback data ', data);
}