Socket.IO client integration in iOS Swift

Today we will start our tutorial on Socket.IO client integration in iOS Swift. Before going any further i’ld like to explain a little about Socket.IO

Socket.IO : What is Socket.IO ??? It enables real-time bidirectional event-based communication. It works on every platform, browser or device, focusing equally on reliability and speed. It supports Real-time analytics, Instant messaging and chat, Binary streaming , Document collaboration. It is Immensely Powerful , yet easy to use. You can download the Socket.IO Client from here.

Socket.IO client integration in iOS Swift

Getting started

Create a new Xcode project and name it whatever you want. The next step is getting the code for Socket.IO Swift you can either use Git to clone the repo to a directory, or simply download from here. Drag the folder named SwiftIO to your project, making sure you select copy.

 

Setting up server

Thats not your part to do, web developer will do that for you most of the time. If you want your own then you can have it locally. We are not concentrating on the server part so lets move forward. Next time i’ll post tutorial on how to setup Socket.IO server locally.

Connecting to the Server

We will provide the IP address of our server and the designated port.

And for connecting to Socket.IO we have to just write single line.

Similarly for disconnecting from server.

see so simple just one word to connect and one word to disconnect from server.

Adding handlers

Now, in our overridden viewDidLoad method we want to add two things, a method call that will add our handlers to our socket, and a call to connect our socket.

Now that we have our method calls, it’s time to implement the method that adds the handlers!

These are like delegate methods which are called automatically when server send something or updates. In the method i’m updating my table view with the chat received.

 

Sending data to server

Now you must be wondering how to send data to Socket.IO server from our iOS app. This is also simple just like above methods.

The above line will add a user named “me” to the chat room.

If you want to send multiple variable.

Sometime we have to send some complex data like a json, that is also simple.

We can also send binary data

 

Requesting Acks

Yes, you can also request acks from Socket.IO server.

 

I hope our tutorial on Socket.IO client integration in iOS Swift covers most of the basics but if you want any updation post your comment below. Happy Coding 🙂

 

3 Comments

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.