Git Product home page Git Product logo

Comments (5)

mateodelnorte avatar mateodelnorte commented on September 6, 2024 3

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.

gdaws avatar gdaws commented on September 6, 2024 2

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.

gdaws avatar gdaws commented on September 6, 2024 1

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.

choudhary001 avatar choudhary001 commented on September 6, 2024

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.

naxxfish avatar naxxfish commented on September 6, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.