Message getting published endlessly with MQTT and Node.js - node.js

I have a mosquitto broker running on one of my machines (say: mqtt://10.0.0.50:1883).
I am trying to use MQTT with Node in an IoT gateway to receive messages from a mobile app and based on the topic for which message was sent perform some function. Once the action is complete publish another message on the same topic from gateway for mobile apps.
Gateway side:
client.on('connect', function () {
if (client.connected) {
client.subscribe(topics, {qos: 1}, function (err, granted) {
console.log('Subscribed to topics: ' + JSON.stringify(granted));
});
}
});
client.on('message', function (topic, message) {
switch (topic.toUpperCase()) {
case 'TEST':
console.log('test topic Called');
client.publish(topic, "test topic response msg", {qos: 1},
function () {
console.log("message response sent");
});
}
});
I tried this piece of code with another sample node.js code.
Client side:
client.on('connect', function () {
if (client.connected) {
client.subscribe("test", {qos: 1}, function (err, granted) {
console.log('Subscribed to topics: ' + JSON.stringify(granted));
});
client.publish("test", "send_me_test", {qos: 1}, function () {
console.log("request message published");
});
}
});
client.on('message', function (topic, message) {
console.log("Topic: " + topic + " request: " + message);
});
Response that I get after running client.js:
Connected to the broker at: mqtt://10.0.0.50:1883
Subscribed to topics: [{"topic":"test","qos":1}]
message published
Topic: test request: send_me_test
Topic: test request: test topics response msg
Topic: test request: test topics response msg
Topic: test request: test topics response msg
which goes on endlessly.
On the gateway side:
Jun 21 08:53:56 intel-quark api-server[16975]: test topic Called
Jun 21 08:53:56 intel-quark api-server[16975]: publishing response for topic: test with data: "test topics response msg"
Jun 21 08:53:56 intel-quark api-server[16975]: message published
keeps on repeating endlessly.
I am not able to figure out why it's happening likewise. My desired output was one response publish for one request publish.

Your gateway is subscribing to the same topic as it is publishing on to so since it will receive it's own messages these will just loop for ever.
Responding on the same topic that a message is received on is not normally a good idea unless the message body has something in it to mark it as a response that can be checked to prevent loops like this.

Related

How can i realize http-streaming of kafka messages to endpoint on Nodejs?

I've been trying to realize stream kafka messages by get endpoint on client side using http-streaming.
Thats my kafka consumer, where i recieve messages from producer
var consumer = new Kafka.KafkaConsumer({
'group.id': 'kafka',
'metadata.broker.list': 'localhost:9092',
}, {});
consumer.connect();
consumer.on('ready', () => {
console.log('consumer ready..')
consumer.subscribe(['test']);
consumer.consume();
}).on('data', function(data) {
console.log(`received message: ${eventType.fromBuffer(data.value)}`);
});
I need to stream this messages by endpoint. How can i do that?
I tryed do that on Koa and express, I just put this code into get endpoint

sending messages to specific Client with node-ipc

I have an electron app where different clients communicate with a server over a Network using node-ipc.
As long as the client connects to the server first it is no problem to answer that specific client. According to the docs I have on the Server:
NodeIpc.serveNet(
function () {
NodeIpc.server.on(
'Client.message',
function (data, socket) {
NodeIpc.log('got a message from', (data.id), (data.message));
NodeIpc.server.emit(
socket,
'ServerData',
{
message: 'have some initial cofiguration data'
}
);
);
NodeIpc.server.start();
}
and on my Client:
NodeIPC.connectToNet(
'world',
function(){
NodeIPC.of.world.on(
'connect',
function(){
NodeIPC.log('## connected to world ##', NodeIPC.config.delay);
NodeIPC.of.world.emit(
'Client.message',
{
id : UniqueClientID,
message : 'Some information about myself'
}
);
});
});
That works great, but I cannot figure out how to push some additional information to a specific client some time Later. Trying
NodeIpc.server.of['UniqueClientID'].emit(
'additional Data',
{
message: 'some more configuration'
}
)
});
and
NodeIpc.server.emit(
UniqueClientID,
'additional Data',
{
message: 'some more configuration'
});
do not work.
Does anyone know how to send a message to a given client? (of course there is always the possibility to broadcast that message and let the client decide if it's for him, but I would prefer to talk to the directly)

Message not receiving after publishing. RABBITMQ MQTT

Am learning MQTT and facing some issues understanding MQTT with RabbitMQ from http://blog.airasoul.io/the-internet-of-things-with-rabbitmq-node-js-mqtt-and-amqp/.
So, the issue here is when I run publisher code, a queue is added mqtt-subscription-test-qos1 but when I message doesn't get added in that queue. Although I've added binding of amq.topic to this queue with key-binding 'presence'.
This is my publisher code
var payload = {
message : 'Hello'
};
var client = mqtt.connect(url, { clientId: 'test-', clean:true});
client.on('connect', function () {
client.publish('presence', JSON.stringify(payload), { qos: 1 }, function() {
console.log("Sent");
client.end();
process.exit();
});
});
and below is my subscriber code.
var client = mqtt.connect(url, { clientId: 'test-', clean:true});
client.on('connect', function () {
client.subscribe('presence', { qos: 1 });
});
client.on('message', function (topic, message) {
console.log('received message ', message.toString());
});
This works, when I don't declare any options with connect function in publisher code. So what I don't get is, isn't publisher supposed to create a queue and then publish to topics?
What am I doing wrong?
You don't need to create a queue before publishing to the topic. When you publish first MQTT message, a queue gets created automatically with the default exchange name "amq.topic" and binding key same as your topic name.
I suspect your subscriber is not receiving the messages published since it starts and subscribes to the topic AFTER the publisher publishes the messages. Try by starting your subscriber first and then start your publisher.

Mosca sends multiple messages continously

I have setup a node js server with Mosca running on it. Clients are able to connect to the Mosca server and publish a message. I need to the send an acknowledgment in the form of a message(subscribed to some topic) back to the client.
The below code sends multiple messages continuously once the message is published by the client. Am I missing anything?
var settings = {
port: 1882,
backend: ascoltatore
};
var message = {
topic: 'crofters',
payload: 'OK', // or a Buffer
qos: 2
};
var server = new mosca.Server(settings);
server.on('clientConnected', function(client) {
console.log('client connected', client.id);
});
// fired when a message is received
server.on('published', function(packet, client ) {
var packet_payload = packet.payload;
packet_payload = packet_payload.toString();
console.log('Published', packet_payload);
server.publish(message, function() {
console.log('done!');
});
});
server.on('ready', setup);
function setup() {
console.log('Mosca server is up and running');
}
The event listener server.on('published', function(packet, client){...} listens to every publishing events, including the server's.
What is happening is that when you use server.publish(message, function(){...}) inside that listener it triggers another published event, which is immediately caught by the listener.
It never stops publishing because it never stops catching its own events.
I have been facing similar issues. If you notice, Mosca has only QoS 0 and Qos 1
So I suppose the broker tries to send the same message more than once "at least once" until it receives some acknowledgement from a client. Check this document out

Reactjs don't duplicate my message after update state

I was following the example this blog and the question arose me after implementing
https://github.com/DanialK/ReactJS-Realtime-Chat
Summarizing, before send a message via websocket the state of messages is updated.
And when server receives that message, they send a broadcast to all clients, indluding myself.
Thereat, client updates the state with this same message
Why this message does not appear 2 times?
I don't want that message appear 2 times, but I want to know why it happens
Client code:
socket.on('send:message', this.messageRecieve);
...
handleMessageSubmit : function(message){
Messages.push(message);
this.setState({ messages : Messages });
socket.emit('send:message', message);
},
messageRecieve: function(message){
Messages.push(message);
this.setState({ messages : Messages });
},
Server code:
socket.on('send:message', function (data) {
socket.broadcast.emit('send:message', {
user: name,
text: data.text
});
});
The server does not send the message to the connected user.
As per the socket.io documentation, this API sends to all but the owner of the socket:
socket.broadcast.emit('send:message')
https://github.com/DanialK/ReactJS-Realtime-Chat/blob/master/routes/socket.js

Resources