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 pass connection options like username and password #8

Open
gdegas opened this issue Mar 14, 2018 · 10 comments
Open

How to pass connection options like username and password #8

gdegas opened this issue Mar 14, 2018 · 10 comments

Comments

@gdegas
Copy link

gdegas commented Mar 14, 2018

How can I do this, all it's letting me to do is pass a string with the url of the broker to connect to but not configuration options

@lenlch
Copy link

lenlch commented May 17, 2018

@gdegas Did you resolver this problem? thanks, I have the same problem

@gdegas
Copy link
Author

gdegas commented Jun 1, 2018

@lenlch No I defaulted to using mqtt.js

@hadifikri
Copy link

hadifikri commented Aug 3, 2018

mqtt-react is a container for mqttjs/MQTT.js, it is possible to include the options based on the docs:

import { Connector } from 'mqtt-react';
import App from './components/App';

export default () => (
  <Connector mqttProps="ws://username:[email protected]">
    <App />
  </Connector>
);

URL.parse() function from here

┌─────────────────────────────────────────────────────────────────────────────────────────────┐
│                                            href                                             │
├──────────┬──┬─────────────────────┬─────────────────────┬───────────────────────────┬───────┤
│ protocol │  │        auth         │        host         │           path            │ hash  │
│          │  │                     ├──────────────┬──────┼──────────┬────────────────┤       │
│          │  │                     │   hostname   │ port │ pathname │     search     │       │
│          │  │                     │              │      │          ├─┬──────────────┤       │
│          │  │                     │              │      │          │ │    query     │       │
"  https:   //    user   :   pass   @ sub.host.com : 8080   /p/a/t/h  ?  query=string   #hash "
│          │  │          │          │   hostname   │ port │          │                │       │
│          │  │          │          ├──────────────┴──────┤          │                │       │
│ protocol │  │ username │ password │        host         │          │                │       │
├──────────┴──┼──────────┴──────────┼─────────────────────┤          │                │       │
│   origin    │                     │       origin        │ pathname │     search     │ hash  │
├─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────────┴───────┤
│                                            href                                             │
└─────────────────────────────────────────────────────────────────────────────────────────────┘
(all spaces in the "" line should be ignored — they are purely for formatting)

A simpler way:

<Connector mqttProps={
    {
        host: 'test.mosca.io',
        port:3000,
        username: 'username',
        password: 'password'
    }
}>

By default, mqtt.js would simply used ws protocol

Tested using mosca mqtt broker with authentication enabled

@Abenezer
Copy link

for other options (non url related) like clientId ... 'options' props should have been defined.. (just pass this prop as a second argument for MQTT.connect() )
any ways since Connector also support directly specifying mqtt object through 'mqtt' prop .. we cal also do it this way.

import MQTT from "mqtt"; const mqtt = MQTT.connect('url', options); <Connector mqtt={mqtt}>

@deegha
Copy link

deegha commented Feb 2, 2019

I have not tested this library yet. But since mqtt-react is a container for mqttjs/MQTT.js, it should be possible to include the options based on the docs:

import { Connector } from 'mqtt-react';
import App from './components/App';

export default () => (
  <Connector mqttProps="ws://username:[email protected]">
    <App />
  </Connector>
);

URL.parse() function from here

┌─────────────────────────────────────────────────────────────────────────────────────────────┐
│                                            href                                             │
├──────────┬──┬─────────────────────┬─────────────────────┬───────────────────────────┬───────┤
│ protocol │  │        auth         │        host         │           path            │ hash  │
│          │  │                     ├──────────────┬──────┼──────────┬────────────────┤       │
│          │  │                     │   hostname   │ port │ pathname │     search     │       │
│          │  │                     │              │      │          ├─┬──────────────┤       │
│          │  │                     │              │      │          │ │    query     │       │
"  https:   //    user   :   pass   @ sub.host.com : 8080   /p/a/t/h  ?  query=string   #hash "
│          │  │          │          │   hostname   │ port │          │                │       │
│          │  │          │          ├──────────────┴──────┤          │                │       │
│ protocol │  │ username │ password │        host         │          │                │       │
├──────────┴──┼──────────┴──────────┼─────────────────────┤          │                │       │
│   origin    │                     │       origin        │ pathname │     search     │ hash  │
├─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────────┴───────┤
│                                            href                                             │
└─────────────────────────────────────────────────────────────────────────────────────────────┘
(all spaces in the "" line should be ignored — they are purely for formatting)

Simpler way:

<Connector mqttProps={
    host: 'ws://test.mosca.io',
    username: 'username',
    password: 'password'
  }
>

am pretty sure you meant this to be like this
<Connector mqttProps={
{
host: 'ws://test.mosca.io',
username: 'username',
password: 'password'
}
}>

i tried this but didn't work

@hadifikri
Copy link

I have not tested this library yet. But since mqtt-react is a container for mqttjs/MQTT.js, it should be possible to include the options based on the docs:

import { Connector } from 'mqtt-react';
import App from './components/App';

export default () => (
  <Connector mqttProps="ws://username:[email protected]">
    <App />
  </Connector>
);

URL.parse() function from here

┌─────────────────────────────────────────────────────────────────────────────────────────────┐
│                                            href                                             │
├──────────┬──┬─────────────────────┬─────────────────────┬───────────────────────────┬───────┤
│ protocol │  │        auth         │        host         │           path            │ hash  │
│          │  │                     ├──────────────┬──────┼──────────┬────────────────┤       │
│          │  │                     │   hostname   │ port │ pathname │     search     │       │
│          │  │                     │              │      │          ├─┬──────────────┤       │
│          │  │                     │              │      │          │ │    query     │       │
"  https:   //    user   :   pass   @ sub.host.com : 8080   /p/a/t/h  ?  query=string   #hash "
│          │  │          │          │   hostname   │ port │          │                │       │
│          │  │          │          ├──────────────┴──────┤          │                │       │
│ protocol │  │ username │ password │        host         │          │                │       │
├──────────┴──┼──────────┴──────────┼─────────────────────┤          │                │       │
│   origin    │                     │       origin        │ pathname │     search     │ hash  │
├─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────────┴───────┤
│                                            href                                             │
└─────────────────────────────────────────────────────────────────────────────────────────────┘
(all spaces in the "" line should be ignored — they are purely for formatting)

Simpler way:

<Connector mqttProps={
    host: 'ws://test.mosca.io',
    username: 'username',
    password: 'password'
  }
>

am pretty sure you meant this to be like this
<Connector mqttProps={
{
host: 'ws://test.mosca.io',
username: 'username',
password: 'password'
}
}>

i tried this but didn't work

<Connector mqttProps={
    {
        host: 'test.mosca.io',
        port:3000,
        username: 'username',
        password: 'password'
    }
}>

Remove the protocol on host leaving only the base URL, then specify the port separately. Tested on mosca mqtt broker with authentication enabled.

@AarjuRabadiya
Copy link

AarjuRabadiya commented Jul 20, 2020

Hello @gdegas,
I am getting failed: Error during WebSocket handshake: net:: ERR_CONNECTION_RESET this error when I fixed mqttprops.

I tried using these props

mqttProps: {
      host: "157.245.85.49:8084/mqtt",
      username: "tolomoto",
      password: "S36EeqD956UwGCYC",
    },

or

 mqttProps: {
     protocol: "wss",
       host: "wss://157.245.85.49:8084/mqtt",
      port: "8084",
       username: "tolomoto",
       password: "S36EeqD956UwGCYC",
     },

or
mqttProps: "ws://157.245.85.49:8084/mqtt"

or

mqttProps: "ws://tolomoto:[email protected]:8084/mqtt

Please tell me how to solve it.

@hadifikri
Copy link

hadifikri commented Jul 20, 2020

Hello @gdegas,
I am getting failed: Error during WebSocket handshake: net:: ERR_CONNECTION_RESET this error when I fixed mqttprops.

I tried using these props

mqttProps: {
      host: "157.245.85.49:8084/mqtt",
      username: "tolomoto",
      password: "S36EeqD956UwGCYC",
    },

or

 mqttProps: {
     protocol: "wss",
       host: "wss://157.245.85.49:8084/mqtt",
      port: "8084",
       username: "tolomoto",
       password: "S36EeqD956UwGCYC",
     },

or
mqttProps: "ws://157.245.85.49:8084/mqtt"

or

mqttProps: "ws://tolomoto:[email protected]:8084/mqtt

Please tell me how to solve it.

mqttProps: {{   
      host: "157.245.85.49:8084/mqtt",
      username: "tolomoto",
      password: "S36EeqD956UwGCYC",
}}  
mqttProps: {"ws://157.245.85.49:8084/mqtt"}
mqttProps: {"ws://tolomoto:[email protected]:8084/mqtt}

Make sure you have the curly braces...

mqttProps: {{
       protocol: "wss",
       host: "wss://157.245.85.49:8084/mqtt",
       port: "8084",
       username: "tolomoto",
       password: "S36EeqD956UwGCYC",
  }}

This would never work. The component do not looks for protocol. Specifying the protocol like this host:"wss://" is not working either. By the default protocol ws is used. If your are going to use wss, use URL based:

mqttProps: {"wss://tolomoto:[email protected]:8084/mqtt} works.

@AarjuRabadiya
Copy link

AarjuRabadiya commented Jul 21, 2020

Hello @gdegas,
I am getting failed: Error during WebSocket handshake: net:: ERR_CONNECTION_RESET this error when I fixed mqttprops.
I tried using these props

mqttProps: {
      host: "157.245.85.49:8084/mqtt",
      username: "tolomoto",
      password: "S36EeqD956UwGCYC",
    },

or

 mqttProps: {
     protocol: "wss",
       host: "wss://157.245.85.49:8084/mqtt",
      port: "8084",
       username: "tolomoto",
       password: "S36EeqD956UwGCYC",
     },

or
mqttProps: "ws://157.245.85.49:8084/mqtt"
or
mqttProps: "ws://tolomoto:[email protected]:8084/mqtt
Please tell me how to solve it.

mqttProps: {{   
      host: "157.245.85.49:8084/mqtt",
      username: "tolomoto",
      password: "S36EeqD956UwGCYC",
}}  
mqttProps: {"ws://157.245.85.49:8084/mqtt"}
mqttProps: {"ws://tolomoto:[email protected]:8084/mqtt}

Make sure you have the curly braces...

mqttProps: {{
       protocol: "wss",
       host: "wss://157.245.85.49:8084/mqtt",
       port: "8084",
       username: "tolomoto",
       password: "S36EeqD956UwGCYC",
  }}

This would never work. The component do not looks for protocol. Specifying the protocol like this host:"wss://" is not working either. By the default protocol ws is used. If your are going to use wss, use URL based:

mqttProps: {"wss://tolomoto:[email protected]:8084/mqtt} works.

Hello @gdegas, @hadifikri,
mqttProps: "wss://tolomoto:[email protected]:8084/mqtt" -> This is also not working

@hadifikri
Copy link

hadifikri commented Jul 21, 2020

Hello @gdegas, @hadifikri,
mqttProps: "wss://tolomoto:[email protected]:8084/mqtt" -> This is also not working

const mqtt_username = 'uname';
const mqtt_password = 'pass';

const mqtt_broker_ws = `ws://${mqtt_username}:${mqtt_password}@157.245.85.49:8084/mqtt`;
const mqtt_broker_wss = `wss://${mqtt_username}:${mqtt_password}@1157.245.85.49:8084/mqtt`;

<Connector mqttProps={mqtt_broker_ws}>

</Connector>

or 

<Connector mqttProps={mqtt_broker_wss}>

</Connector>

If it's not working still, please check on how you serve the service at 157.245.85.49:8084/mqtt. What kind of webserver do you use? The problem might be on the broker side. I notice you tried both ws and wss on the same port number. Do take note that a single port can only handle one protocol, not both.

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

No branches or pull requests

6 participants