Unable to connect multiple devices over peer to peer by using Bonjour

Hi, I am implementing an iPhone/iPad and mac app that allows users to send each other messages using the bonjour protocol. Basically, a server publishes his service over bonjour and the clients connected to the same wifi can discover his service and connect to it to start sending messages, it works fine with client and service communication but if I want to achieve peer to peer communication between multiple devices then I don't know how can I get list of connected Devices in network and how multiple devices can communicate with each other like peer to peer connectivity without using server/host?

So how can be this possible?

Look at "Multipeer Connectivity", which supports peer-to-peer connectivity and the discovery of nearby devices.

https://developer.apple.com/documentation/multipeerconnectivity

Yes, I checked "Multipeer Connectivity" but I need peer to peer connectivity between iOS and Android which supports cross platform connectivity.

"Multipeer Connectivity" only available in iOS.

Can anyone please guide me how can be this possible?

Thanks

"Multipeer Connectivity" only available in iOS.

Correct.

Well, it’s better to say that it’s only available on Apple platforms [1].

Also, while Multipeer Connectivity has its place, in many cases it’s the wrong choice. You wrote:

Basically, a server publishes his service over bonjour and the clients connected to the same wifi can discover his service

This asymmetric design is a poor match for Multipeer Connectivity and is better served by an implementation that uses Bonjour and our standard TCP/IP APIs.

I am implementing an iPhone/iPad and mac app that allows users to send each other messages using the bonjour protocol.

Just to clarify, Bonjour is not a protocol. Rather, it’s a marketing term for a number of different protocols:

  • RFC 3927 Dynamic Configuration of IPv4 Link-Local Addresses

  • RFC 6762 Multicast DNS

  • RFC 6763 DNS-Based Service Discovery

These protocols let you advertise and discover services but they do not you communicate with those services. For that you’ll need a standard TCP/IP API.

I don't know how can I get list of connected Devices in network

Bonjour does not work in terms of devices but rather in terms of services. For example, you don’t browse for devices and then ask each device what services it’s running. Rather, you browse for services directly.

Our general advice on this front is you use Network framework for this sort of thing. It includes well-integrated support for both Bonjour and TCP (or UDP). Specifically:

  • Use NWListener to listen for incoming connections. Set the service to enable Bonjour advertisement.

  • Use NWBrowser to browse for services.

  • Use NWConnection to connect to a service.

This gives you a TCP connection between your client and server.


The above is available on all Apple platforms [1]. Non-Apple platforms also support this model, but their Bonjour APIs aren’t integrated, that is, they offer separate Bonjour and TCP/IP APIs and you have to do the integration yourself.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Except watchOS, where the story is more complex.

I have already implemented Bonjour with GCDAsyncSocket to make communication between client and server for selected services, it works fine. Yes, Bonjour is cross platform and will use separate Bonjour and TCP/IP APIs for different platforms.

But I want to also make peer to peer communication in local network with cross platform connectivity technology(Apple and android). I tired to achieve this by bonjour but did not get succeed on it.

Yes, Bonjour does not work in terms of devices but rather in terms of services. So is there any other way to achieve peer to peer communication for Apple and Android platform(cross platform).

Thanks

This centres on what you mean by “peer to peer”. In this context there are two common definitions:

  • Devices within the same multicast and unicast domain — That is, the devices can communicate via unicast but also, if one sends a local multicast, the other will see it. For example:

    • Devices associated with the same Wi-Fi network where the network doesn’t go out of its way to block Bonjour

    • Devices on the same Ethernet

    • One device on Ethernet and one device on Wi-Fi where the Ethernet and Wi-Fi are bridged

  • Otherwise

In the first case Bonjour is entirely defined by Internet RFCs. Non-Apple platforms can implement those RFCs and will be able to communicate with Apple devices.

In the second case, Apple uses proprietary technology that enables Bonjour over peer-to-peer Wi-Fi [1]. For example, two iPhones can discovery each other via Bonjour even if they are not associated with the same Wi-Fi network. The mechanism used to do this is not documented for third-party use and thus not available on other platforms.

Clear?

Finally, if you’re concerned with the first case and you’re asking about Bonjour APIs on non-Apple platforms, I recommend that you escalate that via the support channel for those platforms. My understanding is that Android supports Bonjour via Avahi, but I’m not the right person to give you definitive answers about Android.

ps You wrote:

I have already implemented Bonjour with GCDAsyncSocket

You should seriously consider moving to Network framework for this. GCDAsyncSocket is based on CFSocketStream, which is no longer mainstream. For example, the TLS 1.3 support we rolled out last year is not available to CFSocketStream clients. Also note that we’ve formally deprecated CFSocketStream in this year’s SDKs.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Historically we also supported doing this over Bluetooth, but that’s no longer supported.

Hi, Thanks for your quick reply.

I will look into Network framework instead of GCDAsyncSocket.

But the main question/problem is still remain which is that how to implement peer to peer communication between iOS and Android devices in local network.

Thanks

But the main question/problem is still remain which is that how to implement peer to peer communication between iOS and Android devices in local network.

By “in local network” do you mean:

  • Devices within the same multicast and unicast domain

  • Otherwise

It’s hard to provide further guidance without nailing down that detail.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

By "in local network" I mean: "Devices within the same multicast and unicast domain"

I mean: "Devices within the same multicast and unicast domain"

Cool. In that case supporting non-Apple platforms is straightforward. On the Apple platform [1] you need to:

  • Write the code to communicate using the network transport protocol of your choice (to keep things simple I’m going to assume TCP, but there’s nothing fundamentally incompatible with UDP here). I recommend Network framework for this, but it seems that you already have existing code using GCDAsyncSocket and that’s fine.

  • If your code is listening for TCP connections, advertise that listener using a Bonjour API. Network framework has built-in support for this. I’m not sure what the situation is with GCDAsyncSocket. If you have questions about that library, consult its support resources. And if all else fails you can advertise using NSNetService or <dns_sd.h>.

  • If your code is making outgoing TCP connections, implement the Bonjour browse operation. For Network framework you’d use NWBrowser. For third-party libraries, consult the resources for that library. And if all else fails, you can use NSNetService or <dns_sd.h>.

  • Outgoing TCP connections also need to implement the Bonjour resolve operation. Again, Network framework has this built in. CFSocketStream also has this built in. Not sure about GCDAsyncSocket. And you can use NSNetService or <dns_sd.h> as a last resort.

With regards non-Apple platforms, I can’t help you with that. The setup I’ve described above is entirely based on Internet standard protocols (TCP, plus the Bonjour RFCs I mentioned upthread). If you have questions about what APIs to use to speak these protocols on a non-Apple platform, you’ll have to consult the support resources for that platform.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Again, the exception here is watchOS.

Unable to connect multiple devices over peer to peer by using Bonjour
 
 
Q