Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Bonjour or Sockets? (or both).. a simple idea..


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 ;)


— asked June 4th 2010 by Kosso
  • awareness
  • bonjour
  • ipad
  • iphone
  • network
  • sockets

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.

    — answered June 4th 2010 by Stephen Tramer
    1 Comment
    • Hi, Thanks Stephen. I've been digging around the KS Bonjour and socket demos, but have yet to reach the elusive 'AHA!' moment ;) I'll get there in the end ;)

      — commented June 4th 2010 by Kosso
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.