Appcelerator Blog

The Leading Resource for All Things Mobile

Titanium Mobile Intro Series: Sockets

0 Flares 0 Flares ×

Welcome to the third part of our series introducing some of the new features coming up in Titanium Mobile 1.7.0. This second installment focuses on the new Titanium.Network.Socket module and TCP socket implementation.


Currently, the Titanium Mobile API does not have a cross platform implementation for sockets, and the addition of the Titanium.Network.Socket module and associated Titanium.Network.Socket.TCP object will fill that void. Initially, only TCP sockets will be supported but UDP sockets will be added in the future. The addition of support for listening and connecting sockets via TCP will allow application developers to incorporate lower level network logic into their applications. Titanium.Network.Socket.TCP implements the Titanium Stream interface and therefore relies on the previously mentioned Titanium.Buffer.

Socket operations are generally asynchronous and therefore socket callbacks are core to how one operation flows into the next. For example, when connecting to a remote host, you would wait until the connected callback was invoked and fire off the read / write operations from inside that callback.

Once a socket is connected, IO operations leave the scope of Ti.Network.Socket and move into the realm of general Stream IO. Again, because sockets are naturally asynchronous, IO interactions with them should generally be conducted through the Ti.Stream module. Lastly, while sockets can be an extremely powerful part of your developer tool box, network programming is non-trivial and investigation into the basics of how BSD sockets work is suggested.

Usage Examples: [gist id=948883] [gist id=948889] [gist id=948897]    

Additional Resources

Kitchen Sink examples:

Socket Specification: Socket Unit Tests:

Buffer Specification: Buffer Unit Tests:

Stream Specification: Stream Unit Tests:

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×


  1. Sander

    You mention:


    Are they all the same? And can they all be used in programming code?

  2. Opie Cyrus


    “Ti” is just a shortcut for “Titanium” so you are able to use both variants in your source. Titanium.Socket should have been Titanium.Network.Socket. The post has been updated accordingly.

  3. Wilson Wijaya


    i want to ask about this part of code above :

    bytesRead =

    is TCPSocket has read function?? because i tried to copy and paste that sample code above and it always stop on this part.

    Thanks in advance

Comments are closed.

Sign up for updates!

Become a mobile leader. Take the first step to scale mobile innovation throughout your enterprise.
Get in touch
computer and tablet showing Appcelerator software
Start free, grow from there.
0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×