Comments (5)
Hi there. Can you please update the documentation to show proper usage of this module? As such, we've run into all sorts of issues with the client API, as it's shown right smack on README.md. I've gone about implementing custom error handling, subscription replay, and am now implementing retries on sends during disconnects... and I came across this issue. Would have loved to have implemented the module as intended, on the first. go.
from stompit.
Sorry for the delayed response.
Most if not all applications of this library should use Channels. The library started with just the Client API and then later added the high-level Channel API, which now supersedes the Client API as it's easier to use. A channel takes care of the connection management and hides transient errors. In contrast the Client API expects the user to call connect and disconnect and it doesn't retry sends or recover subscriptions after an error event. A client object is only valid for one TCP connection. A channel can use multiple TCP connections.
Documentation is lacking, especially around using Channels and there's too much emphasis on using the Client class. I want to update the API reference documentation and perhaps provide some manual documentation.
The ConnectFailver class emits 'connecting', 'connect' and 'error' events which may be useful to an application. The Channel class doesn't emit any useful events. If a terminal error occurs (e.g. exhausted reconnects) in a channel the error is passed through to the given callbacks.
from stompit.
The correct way to use a channel pool is to request a new channel from the pool each time there is a new send/subscribe request. A channel shouldn't be closed by the user. Once a channel is no longer in use it's reclaimed by the pool and then maybe re-used in future channel requests.
A channel factory is a pool with a specific fixed configuration. A factory is configured in such a way that channels are not recycled but are closed as soon as they're reclaimed by the factory. Channels are reclaimed after completion of at least one send or subscription and have no pending sends or subscriptions and the channel is not locked by the user. Use of channel locking is optional.
I recommend using the ChannelPool class directly and setting your preferred options.
If you don't like the behaviour of ChannelPool but want to use channels for their ease of use then you can instantiate a channel (e.g. new stompit.Channel(connManager)
) directly and get full control of its lifecycle.
Here's the fix to your code:
var totalMsgs = 10;
var count = 0;
var delayMs = 1000;
var intervalId;
var channelFactory = new stompit.ChannelFactory(connManager);
var sendParams = {
'destination' : '/queue/myqueue',
'persistent' : 'true'
};
onReceiptMsg = function (error) {
if (error) {
console.log('send error: ' + error.message);
return;
}
else {
console.log ('Received receipt for message from message broker.');
count++;
}
};
onReceiptDisconnect = function () {
if (error) {
console.log('send error: ' + error.message);
return;
}
console.log("Received receipt for DISCONNECT from message broker.");
};
function sendMsg () {
if (count < totalMsgs) {
console.log("Trying to send message " + count);
sendMessage(sendParams, 'Hello number ' + (count), onReceiptMsg);
}
else {
// disconnect after sending count messages by sending DISCONNECT message.
console.log("Trying to send message DISCONNECT");
sendMessage(sendParams, 'DISCONNECT', onReceiptDisconnect);
clearInterval(intervalId);
}
};
intervalId = setInterval(sendMsg, delayMs);
function sendMessage(headers, body, callback) {
channelFactory.channel(function(error, channel) {
if (error) {
callback(error);
return;
}
channel.send(headers, body, callback);
});
}
from stompit.
There are two mechanisms to use Channel API - ChannelFactory or ChannelPool.
Which one should be used? I tried using ChannelPool as per http://gdaws.github.io/node-stomp/api/channel-pool/. But it creates 2 clients by default.
Which one is recommended between these two classes? A documentation of these features would really help. Please let me know if i can help in something.
I tried ChannelFactory as well and tried to send multiple messages but the channel gets closed after sending one message:
var totalMsgs = 10;
var count = 0;
var delayMs = 1000;
var intervalId;
var channelFactory = new stompit.ChannelFactory(connManager);
channelFactory.channel(function(error, channel) {
if (error) {
console.log("channel factory error: " + error.message);
return;
}
var sendParams = {
'destination' : '/queue/myqueue',
'persistent' : 'true'
};
onReceiptMsg = function (error) {
if (error) {
console.log('send error: ' + error.message);
return;
}
else {
console.log ('Received receipt for message from message broker.');
count++;
}
};
onReceiptDisconnect = function () {
if (error) {
console.log('send error: ' + error.message);
return;
}
console.log("Received receipt for DISCONNECT from message broker.");
};
function sendMsg () {
if (count < totalMsgs) {
console.log("Trying to send message " + count);
channel.send(sendParams, 'Hello number ' + (count), onReceiptMsg);
}
else {
// disconnect after sending count messages by sending DISCONNECT message.
console.log("Trying to send message DISCONNECT");
channel.send(sendParams, 'DISCONNECT', onReceiptDisconnect);
channel.close();
clearInterval(intervalId);
}
};
intervalId = setInterval(sendMsg, delayMs);
});
Let me know the correct way to use. I was able to make this code work with Client API.
Thanks,
Abhishek
from stompit.
I can also attest to being confused by this and implementing ChannelFactory in completely the wrong way (I've called channelPool.channel once, then used the subsequent channel object to call send from callbacks, which I now understand will go away as soon as I stop using it the first time around).
Perhaps an example which shows a channel being requested and then using it to send a message might help make it clearer?
from stompit.
Related Issues (20)
- Error: Unexpected ACK received for message-id. How to resolve? HOT 2
- Bulk processing HOT 2
- Cannot send any message when applying the heavy load in sequential in for loop
- Connection refused 127.0.0.1:61613 HOT 3
- How to remove activemq queues and scheduled jobs?
- How to get scheduled id from client when scheduling message to activemq?
- How to set type in message headers
- Unable to use priority in activeMq using stompit in nodejs HOT 1
- Use of Heartbeat in activeMQ HOT 1
- Message gets redelivered in loop during the time of high CPU processing on the client side HOT 3
- Is there a way to programmatically delete an ActiveMQ Job Schedule? HOT 1
- Disconnect hangs after unsubscribe HOT 1
- The connection is closing when thousands of messages are ready to subscribe in queue HOT 1
- Max size of message
- Setting AMQ_SCHEDULED_DELAY doesn't work
- Async usage?
- Cannot read properties of undefined (reading 'send')
- Get properties of message
- How to keep the connection open and also set connection timeout?
- A NACK call incorrectly results in an ACK on the server side
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stompit.