Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to connect mqtt with socket header options? #531

Closed
sinhpn92 opened this issue Apr 12, 2024 · 3 comments
Closed

How to connect mqtt with socket header options? #531

sinhpn92 opened this issue Apr 12, 2024 · 3 comments

Comments

@sinhpn92
Copy link

Hello team,

I have a web client implemented using React.js to connect to my server. Now, I am developing a mobile app client with Flutter for the same purpose. I have reviewed some examples, but I haven't found a solution to connect to my server using socket header options. My server requires authentication in the header to verify the connection. Below is an example of how I implemented the connection on the React.js client:

const requestOptions = {
        clientId: "mqttclient",
        reschedulePings: false,
        reconnectPeriod: 2000,
        connectTimeout: 60000,
        protocolId: "6xm5cjX78OqX",
        protocolVersion: 3,
        wsOptions: {
            headers: {
                Authorization: "Bearer xxx",
                x-client-id: "iEtJGpzMTgyRe0x4m23lwdFA"
            },
            origin: "https://server.com",
            protocolVersion: 13,
        },
        clean: true,
        keepalive: 20,
        username: "admin",
    };
const client = new mqtt.Client(
        (_) => websocket('wss://...', requestOptions.wsOptions),
        requestOptions
    );

Does the mqtt_client package support this feature? Alternatively, do you have any suggestions on how to implement the connection with this option? Thank you for your assistance.

@shamblett
Copy link
Owner

No the client doesn't allow this, it only supports the setting of web protocols although server side web sockets in Dart do support this in the connect method -

static Future<WebSocket> connect(String url,
          {Iterable<String>? protocols,
          Map<String, dynamic>? headers,
          CompressionOptions compression =
              CompressionOptions.compressionDefault,
          HttpClient? customClient}) =>
      _WebSocketImpl.connect(url, protocols, headers,
          compression: compression, customClient: customClient);

This is a bit naughty really, the MQTT protocol doesn't support this kind of authentication, it has its own way of doing this.

The client can be updated to support this seeing as Dart supports it for future use, I'll mark this issue as an enhancement for now.

@sinhpn92
Copy link
Author

Thank you @shamblett for the supporting.

@shamblett
Copy link
Owner

Complete, see the websocketHeader API, package re released at version 10.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants