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

Inject URL in MQTTWebsocketTransport #433

Closed
lagnat opened this issue Feb 26, 2018 · 33 comments
Closed

Inject URL in MQTTWebsocketTransport #433

lagnat opened this issue Feb 26, 2018 · 33 comments

Comments

@lagnat
Copy link

lagnat commented Feb 26, 2018

I imagine I might have to provide an Authorization header or supply the signature in the URL. It this known to work or not work?

@lagnat
Copy link
Author

lagnat commented Feb 28, 2018

I'll answer my question in case anyone else wants to do this.

The solution is to sign the URL with SigV4 as described here. The next problem is to be able to provide the URL to MQTTWebsocketTransport, rather than let MQTTWebsocketTransport construct the url itself. That was accomplished with the following subclass.

@interface MQTTWebsocketIoTTransport : MQTTWebsocketTransport
@property (nonatomic, retain) NSString *url;
@end

@implementation MQTTWebsocketIoTTransport

- (NSURL*) endpointURL {
    NSURL *url = [NSURL URLWithString:self.url];
    return url;
}

I'd be happy to contribute the SigV4 code if you think it makes sense in this framework.

@lagnat lagnat closed this as completed Feb 28, 2018
@jcavar
Copy link
Contributor

jcavar commented Feb 28, 2018

Hi @lagnat. Thanks for this. I think it makes sense that you are able to provide custom url instead of framework constructing it for you.
With that we should be also able to get rid of few parameters which will simplify API.

@jcavar jcavar reopened this Feb 28, 2018
@jcavar jcavar changed the title Is it possible to connect to AWS IoT with just access keys and session? Inject URL in MQTTWebsocketTransport Feb 28, 2018
@jcavar
Copy link
Contributor

jcavar commented Feb 28, 2018

So for now, I would suggest to only implement this so you don't need your custom subclass. For SigV4 part I would rather leave it out to avoid additional complexity, but it would be nice to document it if someone else needs it.

@mendirattanishant
Copy link
Contributor

@lagnat how did you inject this URL to WSSTransport layer after subclassing?

@lagnat
Copy link
Author

lagnat commented Apr 13, 2018

More or less like this:

  MQTTWebsocketIoTTransport *transport = [[MQTTWebsocketIoTTransport alloc] init];
  transport.url = @" the url ";

   MQTTSession  *client = [[MQTTSession alloc] init];
   client.transport = transport;
   client connectAndWaitTimeout:30];

@yusufonderd
Copy link

Hi @lagnat I need to MQTTWebsocketIoTTransport class. Can you help me ? I have a presigned url and I can't use it.

@jcavar
Copy link
Contributor

jcavar commented May 9, 2018

Which part do you have problem with?

@yusufonderd
Copy link

Check this : #458

@jcavar
Copy link
Contributor

jcavar commented May 9, 2018

It doesn't seem like you have created subclass?

@yusufonderd
Copy link

yusufonderd commented May 9, 2018

Yes I created subclass but I don't sure it is true.
My sub class :

import MQTTClient
class MQTTWebsocketIoTTransport: MQTTWebsocketTransport {
    var url : String = ""
    func endPointUrl() -> URL {
        return URL(string: url)!
    }
}
 

@lagnat
Copy link
Author

lagnat commented May 9, 2018

I suspect your problem is the name of your endPointUrl function. It should be endpointURL, but I don't know swift so I might be wrong about how it bridges.

@jcavar
Copy link
Contributor

jcavar commented May 9, 2018

Yes, Swift will as well require you to add override in front of function.

@yusufonderd
Copy link

yusufonderd commented May 10, 2018

MQTTWebsocketTransport object doesn't contains endpointURL function. You can check from source code: https://github.com/novastone-media/MQTT-Client-Framework/blob/fad0943ddc1c96b5cecc68314f540ac1097b870e/MQTTClient/MQTTClient/MQTTWebsocketTransport/MQTTWebsocketTransport.h . I don't know. It should not be that hard. I only want connect aws presigned url with this library : #458 . By the way, I'm using Eclipse Paho library in Android side and It works fine.

@yusufonderd
Copy link

yusufonderd commented May 10, 2018

Thank you now I edited subsclass as below :

class MQTTWebsocketIoTTransport: MQTTWebsocketTransport {
    var url : String = ""
    func endpointURL() -> NSURL {
        return NSURL(string: url)!
    }
}

Then I tried to connect :

```
        let transport = MQTTWebsocketIoTTransport()
            transport.url = websocketUrl
            let  client = MQTTSession()
            client?.transport = transport;
            client?.clientId = clientID
            client?.delegate = self
            client?.connectionHandler = { session in
                print("session:\(session)")
            }
            client?.connectAndWaitTimeout(10)

And I show this logs : 

> [MQTTSession] init
> [MQTTSession] connecting
> [MQTTSessionSynchron] waiting for connect
> [MQTTSessionSynchron] waiting for connect
> [MQTTSessionSynchron] waiting for connect
> [MQTTSessionSynchron] waiting for connect
> [MQTTSessionSynchron] waiting for connect
> [MQTTSessionSynchron] waiting for connect
> [MQTTSessionSynchron] waiting for connect
> [MQTTSessionSynchron] waiting for connect
> [MQTTSessionSynchron] waiting for connect
> [MQTTSessionSynchron] waiting for connect
> [MQTTSessionSynchron] waiting for connect
> [MQTTSessionSynchron] waiting for connect
> [MQTTSessionSynchron] waiting for connect
> [MQTTSessionSynchron] waiting for connect
> [MQTTSessionSynchron] waiting for connect
> [MQTTSessionSynchron] waiting for connect
> [MQTTSessionSynchron] waiting for connect
> [MQTTSessionSynchron] waiting for connect
> [MQTTSessionSynchron] waiting for connect
> [MQTTSessionSynchron] end connect
> 

@mendirattanishant
Copy link
Contributor

@yusufonderd did subclassing actually call your endpointURL method? I put a break point and it was never called. Can you try?

@yusufonderd
Copy link

@mendirattanishant You are right. endPointURL method never called.

@jcavar
Copy link
Contributor

jcavar commented May 10, 2018

I think this might be due to Swift bridging.

@jcavar
Copy link
Contributor

jcavar commented May 10, 2018

I am not sure what is best solution for this but it seems like a lot of people have this issues. So as a temporary workaround I would suggest that we introduce endpointURL property and in internal method we check if that property is set and return value or construct it if not.

PR with this would be appreciated.

@jcavar
Copy link
Contributor

jcavar commented May 10, 2018

@mendirattanishant could you create a PR? Instead of commenting out code, I think we just need to check if url is set, otherwise logic stays the same.

@nmendiratta
Copy link

@jcavar Yes, I am working on creating a PR for this.

@jcavar
Copy link
Contributor

jcavar commented May 10, 2018

Nice, thank you!

@yusufonderd
Copy link

@mendirattanishant I tried your solution but it doesn't change anything. Same logs:

[MQTTSession] init
[MQTTSession] connecting
[MQTTSessionSynchron] waiting for connect
[MQTTSessionSynchron] waiting for connect
[MQTTSessionSynchron] waiting for connect
[MQTTSessionSynchron] waiting for connect
[MQTTSessionSynchron] waiting for connect
[MQTTSessionSynchron] waiting for connect
[MQTTSessionSynchron] waiting for connect
[MQTTSessionSynchron] waiting for connect
[MQTTSessionSynchron] waiting for connect
[MQTTSessionSynchron] waiting for connect
[MQTTSessionSynchron] waiting for connect
[MQTTSessionSynchron] waiting for connect
[MQTTSessionSynchron] waiting for connect
[MQTTSessionSynchron] waiting for connect
[MQTTSessionSynchron] waiting for connect
[MQTTSessionSynchron] waiting for connect
[MQTTSessionSynchron] waiting for connect
[MQTTSessionSynchron] waiting for connect
[MQTTSessionSynchron] waiting for connect
[MQTTSessionSynchron] end connect

@mendirattanishant
Copy link
Contributor

@yusufonderd
Can you check if you signed URL contains "/mqtt" path in it?

@yusufonderd
Copy link

Yes my url is signed url and contains '/mqtt' path. I successfully connecting android side with same url.

@yusufonderd
Copy link

I've been working on the same problem for two weeks. Do you have any idea ?

@jcavar
Copy link
Contributor

jcavar commented May 11, 2018

It is hard to say without more info, if you could debug and find more details then it might be more clear.

@nmendiratta
Copy link

@yusufonderd It worked for me after I provided my own url. Please provide more logs for a better clarity.

@yusufonderd
Copy link

yusufonderd commented May 14, 2018

@nmendiratta Do you use aws presigned url ? I will give more detail about problem. But first I have a question. Is anybody use to aws presigned url with this library ?
My url starts as below :

wss://a2xxxxxxxx.iot.us-east-1.amazonaws.com/mqtt?...

@mendirattanishant
Copy link
Contributor

@yusufonderd I use the same pre signed url like
wss://axxxxxxxx.iot.us-east-1.amazonaws.com/mqtt?
with parameters as credentials, etc. In PR #461 I addressed how we can make sue of this library to inject our custom pre signed url and it works for me. Can you please try again?

@yusufonderd
Copy link

yusufonderd commented May 15, 2018

Hi @mendirattanishant thank you for your patient. Now I successfully connected websocket. But I cannot subscribe topic #466

@yusufonderd
Copy link

@mendirattanishant Now I applied your changes again. I'm getting this exception. Any idea for this ?

-[MQTTWebsocketTransport setUrl:]: unrecognized selector sent to instance 0x147e62c50

@mendirattanishant
Copy link
Contributor

@yusufonderd please check if your url is correct.

wsTransport.url = NSURL(string: url) as! URL
session.transport = wsTransport

@jcavar
Copy link
Contributor

jcavar commented Aug 14, 2018

I will close this issue now, we now have support for custom URL. Please open separate issue if you still have it with specific details about it.

@jcavar jcavar closed this as completed Aug 14, 2018
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

5 participants