eclipse / paho.mqtt.java

Eclipse Paho Java MQTT client library. Paho is an Eclipse IoT project.
https://eclipse.org/paho
Other
2.12k stars 884 forks source link

Paho client doesn't support custom headers for MQTT connection over WebSockets #502

Open vit21ik opened 6 years ago

vit21ik commented 6 years ago

When you need connection to MQTT broker using WebSockets, paho client sets default headers:

pw.print("Upgrade: websocket" + LINE_SEPARATOR);
pw.print("Connection: Upgrade" + LINE_SEPARATOR);
pw.print("Sec-WebSocket-Key: " + key + LINE_SEPARATOR);
pw.print("Sec-WebSocket-Protocol: mqtt" + LINE_SEPARATOR);
pw.print("Sec-WebSocket-Version: 13" + LINE_SEPARATOR);

In some cases required send custom headers, for example if you wanted to connect to AWS Iot broker. https://docs.aws.amazon.com/iot/latest/developerguide/custom-auth.html

It would be great add custom headers support to WebSocket handshake request.

If this is a bug regarding the Android Service, please raise the bug here instead: https://github.com/eclipse/paho.mqtt.android/issues/new

vit21ik commented 6 years ago

Actually I did this changes in my forked repository - https://github.com/vit21ik/paho.mqtt.java/commit/7df2eaaf760da97766d3a64049eed4da6f3309da

I could create merge request to develop branch.

jpwsutton commented 6 years ago

Thanks for the pull request, once we merge it would you mind if I ported your changes over to our new MQTTv5 client?

davidgraeff commented 6 years ago

Eclipse Smarthome would use this as well. A near term release including this could help our current mqtt ambitions.

vit21ik commented 6 years ago

Added custom headers to mqtt v5

davidgraeff commented 5 years ago

Can be closed, can't it?

desokroshan commented 5 years ago

@jpwsutton Is there a way to consume these changes via gradle?

aniket91 commented 5 years ago

How do I use this feature? I used the latest dependency in maven-

    <dependency>
        <groupId>org.eclipse.paho</groupId>
        <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
        <version>1.2.0</version>
    </dependency>

But I do not see an option to set custom headers.

desokroshan commented 5 years ago

You can build the jar file from the develop branch as mentioned here and add it to your classpath. These changes will be available as part of 1.3.0 which is overdue.

JacquesSmuts commented 5 years ago

It is now available in version 1.2.1 and you implement it like so:

MqttClient client = new MqttClient("wss://<BROKER_URI>", "MyClient");
MqttConnectOptions connectOptions = new MqttConnectOptions();
Properties properties = new Properties();
properties.setProperty("X-Amz-CustomAuthorizer-Name", <SOME_VALUE>);
properties.setProperty("X-Amz-CustomAuthorizer-Signature", <SOME_VALUE>);
properties.setProperty(<SOME_VALUE>, <SOME_VALUE>);
connectOptions.setCustomWebSocketHeaders(properties);
client.connect(connectOptions);
rrmorey commented 4 years ago

It is now available in version 1.2.1 and you implement it like so:

MqttClient client = new MqttClient("wss://<BROKER_URI>", "MyClient");
MqttConnectOptions connectOptions = new MqttConnectOptions();
Properties properties = new Properties();
properties.setProperty("X-Amz-CustomAuthorizer-Name", <SOME_VALUE>);
properties.setProperty("X-Amz-CustomAuthorizer-Signature", <SOME_VALUE>);
properties.setProperty(<SOME_VALUE>, <SOME_VALUE>);
connectOptions.setCustomWebSocketHeaders(properties);
client.connect(connectOptions);

How can we retrieve the headers on the subscriber side?