0

My goal is to establish peer-to-peer connection between two android devices. To achieve this goal, I chose the protocol mqtt. I would like to start the broker on the first device, and the second one to connect to the first one. I create a broker in this way:

I added these dependencies to the gradle

dependencies {
compile 'io.moquette:moquette-netty-parser:0.8.1'
compile 'io.moquette:moquette-broker:0.8.1'
compile 'io.moquette:moquette-parser-commons:0.8.1'}

Then created a server, but the default URI is tcp://localhost:1883 so the second device I can connect to this broker only if both devices are connected to the same wi-fi point.

broker = new Server();
    try {
        MemoryConfig memoryConfig = new MemoryConfig(new Properties());
        memoryConfig.setProperty(BrokerConstants.PERSISTENT_STORE_PROPERTY_NAME,
                Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator
                        + BrokerConstants.DEFAULT_MOQUETTE_STORE_MAP_DB_FILENAME);
        broker.startServer(memoryConfig);
    } catch (IOException e) {
        e.printStackTrace();
    }

And I Use Paho libraries for android

compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'
compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'

I would like to be able to connect to the broker even if the devices are connected to different points of wi-fi or use 3G/4G.

I honestly tried to find a solution for several days, but I did not succeed. I really do not know how to achieve this. Therefore, I have two questions. Is it possible that I want to do? And How?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Mikhail
  • 57
  • 6
  • Look at this example for MQTT connection. https://stackoverflow.com/questions/43038597/android-studio-mqtt-not-connecting?answertab=active#tab-top This example performs the connect action in an `Activity` but you can do the same in a `Service` – ᴛʜᴇᴘᴀᴛᴇʟ Nov 11 '17 at 20:19

1 Answers1

0

localhost means the this machine so you can only use that URL to connect to the broker on the same device as the broker, not even on the same network/subnet. If you want to connect to a different machine you need to know the ip address of the machine you want to connect to. This leads to a chicken/egg situation of how you find out the IP address of the remote device you want to connect to.

But given mobile networks or home broadband networks tend to behind NAT there will be no way to connect to these devices.

Part of the benefit of a central broker hosted on something like a cloud server is that all the devices can connect to it and share information via the central broker even if they are behind a NAT gateway.

Trying to do direct Peer to Peer with 3G/4G/Home Broadband is not going to be possible.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • And could you tell me how best to implement this with a third-party server? I know that there is a CloudMqtt and my task can be realized with its help, but are there any other options? I will be grateful for any help. – Mikhail Nov 11 '17 at 20:47