stomp-js / rx-stomp

STOMP adaptor for RxJS
Apache License 2.0
110 stars 21 forks source link

rxStomp using JavaScript promises #548

Open mohamedinvite opened 5 months ago

mohamedinvite commented 5 months ago

I coded a way to subscribe to an MQ queue using rxStomp. I have no problems everything works well but I would like to optimize my treatment by making it responsive and using promises but I am not comfortable with this concept in node.js.

I would like to continue to receive messages and send them to the database. But I would like it to send in database is not blocking and that I continue to receive my MQ messages in the order of arrival.

Could someone - help me? Thanks in advance

This is my code actually

function rxSubscribeToTopic() {
    try {

        rxStomp.connected$.subscribe(() => {
            subscription = rxStomp
                .watch({destination: "/queue/test",'ack': 'client', 'activemq.prefetchSize': 1 })
                .subscribe(async (message) => {
                        let client = createMongoClient();
                        try {

                            await client.connect();

                            const database = client.db("stomp");
                            const collection = database.collection("stomp_message");
                            let messageJson = {
                                "message": message.body
                            }
                            await collection.insertOne(messageJson).then(value => console.log("Inserted" + value.insertedId)
                        )
                        } finally {
                            // Ensures that the client will close when you finish/error
                        }
                    }
                );
        })
    } catch (e) {
        log(e)
        rxStomp.stompErrors$.subscribe(value => value.body);
        activateStomp();
    }
kum-deepak commented 5 months ago

I have not tested your code. However, it seems to be doing what you intend. Does it not work as you want it to?

I noticed some other issues with the code. It can be written much simpler. I am ignoring those. I guess that you have written just an outline code in your issue.

mohamedinvite commented 5 months ago

Hello , Kum.

For more details what I want it's to make the function watch() reactive too. actually this code works but i tested with little message. Will it run with big treatment?

My code is a test code but I’m new to JS if you have advice on my code to optimize it will be happy to take them