I am trying to connect to a mqtt brocker using STOMP javascript web socket. The connection is made. But the callback function in my code is not called? But the ping messages are send.
I am using a url as the host address. Here is my code.
<!DOCTYPE html>
<html>
<head>
<script src=".3.4/sockjs.min.js"></script>
<script src="stomp.js"></script>
<script>
var ws = new SockJS('http://108.567.234.143:9876/stomp');
var client = Stomp.over(ws);
client.connect('username', 'pw', connect_callback, on_error);
client.heartbeat.outgoing = 20000; // client will send heartbeats every 20000ms
client.heartbeat.ining = 0; // client does not want to receive heartbeats
// from the server
var connect_callback = function() {
alert("Connected to rabbitMQ");
var subscription = client.subscribe("CRICKET", subs_callback);
console.log('subscribe to CRICKET');
};
var on_error = function(error) {
console.log('error');
};
var subs_callback = function(message) {
// called when the client receives a STOMP message from the server
if (message.body) {
alert("got message with body " + message.body)
//console.log('got message with body' + message.body);
} else {
alert("got empty message");
}
};
console.log('message.body');
</script>
</head>
<body>
hello world
</body>
</html>
I cant subscribe to a topic. Please tell me what is wrong? Here is the console log
Thanks
I am trying to connect to a mqtt brocker using STOMP javascript web socket. The connection is made. But the callback function in my code is not called? But the ping messages are send.
I am using a url as the host address. Here is my code.
<!DOCTYPE html>
<html>
<head>
<script src="http://cdn.jsdelivr/sockjs/0.3.4/sockjs.min.js"></script>
<script src="stomp.js"></script>
<script>
var ws = new SockJS('http://108.567.234.143:9876/stomp');
var client = Stomp.over(ws);
client.connect('username', 'pw', connect_callback, on_error);
client.heartbeat.outgoing = 20000; // client will send heartbeats every 20000ms
client.heartbeat.ining = 0; // client does not want to receive heartbeats
// from the server
var connect_callback = function() {
alert("Connected to rabbitMQ");
var subscription = client.subscribe("CRICKET", subs_callback);
console.log('subscribe to CRICKET');
};
var on_error = function(error) {
console.log('error');
};
var subs_callback = function(message) {
// called when the client receives a STOMP message from the server
if (message.body) {
alert("got message with body " + message.body)
//console.log('got message with body' + message.body);
} else {
alert("got empty message");
}
};
console.log('message.body');
</script>
</head>
<body>
hello world
</body>
</html>
I cant subscribe to a topic. Please tell me what is wrong? Here is the console log
Thanks
Share Improve this question edited May 4, 2016 at 5:41 Yaseen Ahmad 1,7955 gold badges27 silver badges43 bronze badges asked May 4, 2016 at 4:42 sachithsachith 1892 silver badges14 bronze badges 4- Can anyone figure out what is wrong? – sachith Commented May 4, 2016 at 4:44
-
Could it be because you defined the callback function, after you've used it? Try placing
client.connect('username', 'pw', connect_callback, on_error);
after the function definitions – Akurn Commented May 4, 2016 at 4:55 - Thank you very much Akum... It worked.... – sachith Commented May 4, 2016 at 5:01
- Awesome, I'll add it as the answer – Akurn Commented May 4, 2016 at 5:07
1 Answer
Reset to default 3The callbacks are being defined after they're used.
It's likely the .connect()
function is silently ignoring the undefined functions, so you didn't see any errors.
Moving the .connect()
to after the callbacks are defined, should fix the issue.
var ws = new SockJS('http://108.567.234.143:9876/stomp');
var client = Stomp.over(ws);
client.heartbeat.outgoing = 20000; // client will send heartbeats every 20000ms
client.heartbeat.ining = 0; // client does not want to receive heartbeats
// from the server
var connect_callback = function() {
alert("Connected to rabbitMQ");
var subscription = client.subscribe("CRICKET", subs_callback);
console.log('subscribe to CRICKET');
};
var on_error = function(error) {
console.log('error');
};
var subs_callback = function(message) {
// called when the client receives a STOMP message from the server
if (message.body) {
alert("got message with body " + message.body)
//console.log('got message with body' + message.body);
} else {
alert("got empty message");
}
};
client.connect('username', 'pw', connect_callback, on_error);
console.log('message.body');