arloliu / fastmq

High performance message broker implementation for node.js with multiple network transports support.
MIT License
40 stars 6 forks source link

Reconnect with initial failure #8

Closed mpecic closed 4 years ago

mpecic commented 4 years ago

I'm trying to connect to TCP server which may or may not be started, and want to keep trying if it is not. The client code is as follows:

const channelPeer = FastMQ.Client.connect('peer.#', {
    host: '127.0.1.2',
    port: 1234,
    reconnect: true
}, (err, channel) => {
    if (err) {
        console.error('Peer connect error', err);
        return;
    }
    console.log('Channel connected');
    channel.onError((err) => console.error('Peer error', err));
    channel.onReconnect(() => console.log('Peer reconnect'));
}).catch(err => console.error('Peer initial connect error', err)).finally(() => console.log('Promise is done'));

The problem is that if it fails to connect initially, channelPeer promise is never resolved or rejected.

I'll send a pull request with a solution which rejects the promise on initial failure. It works fine for me, but of course only with callback function, as above. Maybe you have some other idea on how to fix this?

arloliu commented 4 years ago

Thanks for sending PR. I had released v1.3.7 for this bug fix