Bonjour or Sockets? (or both).. a simple idea..
Hi,
OK. Let's say I want to create two (almost identical) apps:
When they're both launched (and probably connected via the same wifi network) I want them to be able to 'talk' to eachother
eg: I press a button on one of the apps and the other app on the other device recognises this - and which app it is from. And possibly receive a JS string or JSON 'payload'.
I have a feeling this is possible, but how? Would it use sockets or bonjour, or both?
Any pointers or a simple sample would be great. I've had a neat/wicked/silly/cool idea for something ;)
Cheers,
Kosso
1 Answer
-
Accepted Answer
This is exactly what Bonjour is for. Say we have Device A and Device B:
On startup, Device A advertises a Bonjour service
local.A.service._tcp, and begins listening for other devices which
will also advertise the same service. Device B starts up, advertises
a service local.B.service._tcp, and begins listening as well. At this
point the devices can 'discover' each other via Bonjour (since they're
both looking for service._tcp services in the local. domain) and now
have some information about to communicate.Now, I press button 1 on Device A. This does something on A, but also
sends a message through the socket for the Bonjour service which is
connected to B (which could definitely be a JSON string). B gets this
information (which it can determine is from A via a socket identifier)
and does something with it as well.For an example of how this works, see the Platform -> Bonjour test in KS (and be sure to read both the Bonjour and sockets documentation). Be aware that you'll need to filter out your own device's Bonjour services.