Socket.io in iOS

Recently I read about socket.io. Basically it enables event-based communication between a server and a client. The server side is written in node.js, and they have a lot of client libraries in different languages. The server is open source, under MIT license. You can download it from Github: https://github.com/socketio/socket.io. The concept is really interesting, so I started looking for a solution in iOS.

You can find a few socket.io client libraries out there written in objective-c. But as it turned out, socket.io had released their own client library for iOS. So you should definitely use that. I was more than happy to see, that the library is written is Swift. They also provide objective-c interface. So I fired up Xcode and started a test project. In this article I will show you how to use the library.

Import the client library

The easiest way to import the client library is through Cocoapods. If you didn’t use it before, then you should. It is a most have dependency manager for iOS. So open your Podfile and add the following line:

After that run the pod install command to install the pod. To import the library into your swift file, you simply add ‘import Socket_IO_Client_Swift‘ at the beginning of your class.

Connecting to the server

It is really simple to connect to your server:

In the first row I define a token for authentication. We’ll get back to this a bit later.

In the second row we create the actual socket object. I added some options here. See the available options below.

And in the third row we make the actual connection.

Authentication

Socket.io uses token based authentication. This means you have to send a token with your request. In order to do that, you have to add it to the ConnectParams option.   Of course in a real application this shouldn’t be hard-coded.

Note that this kind of authentication has a big disadvantage: the token is added to the request as HTTP parameter. So even if you use secure communication, a hacker can easily find out the token. I found an article about implementing a better authentication process, but I didn’t have time to give it a try. You can find the article here: https://facundoolano.wordpress.com/2014/10/11/better-authentication-for-socket-io-no-query-strings/

Handling events

In order to handle events you should register to them. You can do this calling the on method on the connection:

You can replace the “connect” with any event you want to handle. Of course these events are sent from the server, so you have to implement them there too. I usually attach these handlers before I connect to the server, so I don’t miss any events.

The data parameter is an array. On its first element contains the content sent by the server.

If you want to execute a handler only once, regardless how many times receive the event, you should use

Also you may want to add a handler to any event you receive. You can do this by calling

the event property holds the name of the event, and the items array contains the data received.

To remove a handler you could call

or if you want to remove every handler then

Sending events

When you want to send an event you have several options: if you want to send a single object and don’t care about the response, you can call

If you have to send multiple objects, you should call

But in some cases you expect to a response from the server. In these case you should call one of the following methods

Namespaces

If you want to join a namespace there are two ways to do it. Either you can specify the namespace when you create the socket, or you can call the appropriate joining method

or

You can also leave a namespace by calling

Note that every namespace should start with ‘/’.