0

I'm trying to use mqtt over websocket in my web application using the Paho JS library to implement notifications. I need to use persistent connection(to receive any pending messages) and for that a unique client id is a must.

Problem 1: Multiple tabs acting as a mqtt client using the same client id and hence disconnecting and connecting in infinite loop(as I've set reconnect to true)

I solved the above problem using Shared Worker which is now using only one connection for all the tabs which is great. But now I've run into another problem.

Problem 2: When I open my application in two different browsers say, Chrome and Firefox, I get different shared workers and hence again two clients using the same clientId resulting in connect/disconnect loop.

Any suggestion would be highly appreciated.

Onex
  • 73
  • 4
  • 1
    Simplest solution: don't open the application in two different browsers - why would you need to? – Jaromanda X May 15 '21 at 09:25
  • @JaromandaX You can't restrict a user from opening the application in multiple browsers. It's one of the test cases. – Onex May 15 '21 at 09:41
  • 1
    you need to consider how is it that two different browsers end up with the same clientId ... where does the clientId come from? do it differently – Jaromanda X May 15 '21 at 12:32
  • I've set the clientId manually for my web application. I've not set it based on the type of browser. I could just append random numbers to my clientId every time I make a connection. But then I won't be able to make a persistent connection. And as a result will lose messages stored on the broker against that clientId – Onex May 15 '21 at 18:08
  • well, you'll need to figure something out then ... perhaps use `navigator.userAgentData?.brands[2].brand || 'Firefox'` ... which will give you a distinct string in Chrome, Edge (Chrome based) and Firefox – Jaromanda X May 16 '21 at 02:22
  • note: currently firefox does not implement `navigator.userAgentData` - I only know that chromium based browsers do ... so, the above may not be the best solution, but it will distinguish between chrome and firefox, the two browsers you mention – Jaromanda X May 16 '21 at 03:01
  • Not sure if this will solve anything, but check out "cotonic" (.org & github). It is the backbone of Zotonic and is essentially a MQTT broker in the browser with web workers connecting to that topic tree. – Pete Moss Oct 29 '21 at 17:56

0 Answers0