I have issue when using npm mqtt with nodejs. Sever subscribe topic 'alert/userId' to receive data publish from client then server unsubscribe this topic . after each subscribe and unsubscribe message is duplicate. The client sends 1 message; the server receives more 1 message.
How did you publish the message? Did you set the retained flag to true?
If so this message will be delivered every time the client connects to the broker until it is cleared (by sending a null payload message to the same topic)
Publishing with QoS 1 means the message will be delivered at least once. Any subscribers could then receive the same message more than once.
You probably want to use QoS 2 if you want the message to be delivered exactly once.
Related
Intro
We're developing a system to support multiple real-time messages (chat) and updates (activity notifications).
That is, user A can receive via Web Socket messages for :
receiving new chat messages
receiving updates for some activity, for example if someone like their photo.
and more.
We use one single WebSocket connection to send all these different messages to the client.
However, we also support multiple applications/clients to be open by the user at the same time.
(i.e - user A connect on their web browser, and also from their mobile app, at the same time).
Architecture
We have a "Hub" that stores a map of UserId to a list of active websocket sessions.
(user:123 -> listOf(session#1, session#2))
Each client, once websocket connection is established, has its own Consumer which subscribes to a pulsar topic "userId" (e.g - user:123 topic).
If user A connected on both mobile and web, each client has its own Consumer to topic user:A.
When user A sends a new message from session #1 to user B, the flow is :
user makes a REST POST request to send a message.
service stores a new message to DB.
service sends a Pulsar message to topic user:B and user:A.
return 200 status code + created Message response.
Problem
If user A has two sessions open (two clients/websockets), and they send a message from session #1, how can we make sure only session #2 gets the message ?
Since user A has already received the 200 response with the created message in session #1, there's no need to send the message to him again by sending a message to his Consumer.
I'm not sure if it's a Pulsar configuration, or perhaps our architecture is wrong.
how can we make sure only session #2 gets the message ?
I'm going to address this at the app level.
Prepend a unique nonce (e.g. a guid) to each message sent.
Maintain a short list of recently sent nonces,
aging them out so we never have more than, say, half a dozen.
Upon receiving a message,
check to see if we sent it.
That is, check to see if its nonce is in the list.
If so, silently discard it.
Equivalently, name each connection.
You could roll a guid just once when a new websocket is opened.
Or you could incorporate some of the websocket's addressing
bits into the name.
Prepend the connection name to each outbound message.
Discard any received message which has "sender" of "self".
With this de-dup'ing approach
there's still some wasted network bandwidth.
We can quibble about it if you wish.
When the K-th websocket is created,
we could create K topics,
each excluding a different endpoint.
Sounds like more work than it's worth!
I'm using the stomp-client library and i want to know if it is possible to know if the message was delivered to the queue. Because im implementing a java service to do the dequeue of the messages and an node js to send the messages to the queue. the code bellow shows how I send the message to the queue.
this._stompClient.publish('/queue/MessagesQueue', messageToPublish, { })
When you send a SEND frame (i.e. publish a message) you can add a receipt header and then when you receive the RECEIPT frame from the broker you know it has successfully received the message. The STOMP specification says this about the receipt header:
Any client frame other than CONNECT MAY specify a receipt header with an arbitrary value. This will cause the server to acknowledge receipt of the frame with a RECEIPT frame which contains the value of this header as the value of the receipt-id header in the RECEIPT frame.
However, looking at the documentation for stomp-client I don't see any mention of how to receive RECEIPT frames. I actually would expect the ability to specify a callback on the publish method which was called when the RECEIPT frame is received. It doesn't appear that stomp-client supports working with receipts. Unfortunately that means there's no real way to confirm the message was received by the broker.
I recommend you find a more mature STOMP client implementation that supports receipts. For example stomp-js supports receipts.
developing chat application using MQTT protocol, mosca (node module) and MongoDB as a database in Node.js
facing the problem of how to delete publish message and remove from all subscriber in app.
At a MQTT level you can't, once a message has been published it's will be delivered by the broker to all connected clients (and queued for disconnected clients) with a matching subscription.
The only thing that is possible is to clear a retained message to prevent the same payload being re-delivered each time the client connects. You do this by publishing a message with a null payload (and the retained bit set)
If you want to delete messages at the chat level you will have to implement this yourself with in the application.
I have PUB/SUB program using zmq broker ( node.js ).
Subscriber doesn't receive messages while subscriber is restarted and publisher is still publishing the messages. But PUB/SUB is working fine when both publisher and subscriber services are started naturally. Reason behind this is unknown.
What could possibly be wrong?
While it is impossible to cover exactly a case of an undisclosed MCVE-design of using a PUB-archetype, on Publisher side, and an unspecified number of SUB-archetype nodes on the Subscribers' sides, there is one important fact.
The Design
Yes, the Design. Having read the API, user will be sure that
ZeroMQ does not guarrantee a message delivery
ZeroMQ PUB-lisher does not wait and publishes a message for all connected SUB-scribers, not waiting for late-joiners, nor providing any queue/persistence for not connected SUB-scribers and discards all messages that were requested to PUB-lish during times there are no SUB-s connected.
Can I have an acknowledgement of delivery when I send a MQTT message with a QOS to 0?
How can I intercept answer with javascript, node.JS ?
You won't get any acknowledgement at the MQTT protocol level from any QOS. The protocol has no concept of a end to end acknowledgement, at QOS1 or QOS2 the handshake is only between the publisher and the broker and then between the broker and the subscriber.
If you want acknowledgement then you need to implement that yourself by publishing a response message that the original publisher can see.
Instead of that you can just subscribe to your own topic and check if you've recieved the message!