Working with socket.io in iOS Swift 3

Bipin Pandey
Resume and CV Builder App
3 min readDec 21, 2017

Usually, most of the iOS/Android apps communicate with servers to exchange data. The server implements and provides RESTful APIs that apps can use for the communication. When an app needs to send data to the server, or fetch from server, it makes the proper request and after a while the data has been returned. That happens several times during the app runtime period. Thankfully the socket.io is the better solution when it’s necessary to receive data from a server instantly (every time such data becomes available), and without having the app to send any request to the server at all.

Writing a realtime application with popular web applications stacks like LAMP has traditionally been very hard that involves polling the server for changes, keeping track of timestamps, and it’s a lot slower than it should be. Socket.IO is a JavaScript library for real-time web applications(Instant messengers, Push Notifications, Online Gaming).

Sockets have traditionally been the solution around which most real-time systems are architected, providing a bi-directional communication channel between a client and a server. This means that the server can push messages to clients. Whenever an event occurs, the idea is that the server will get it and push it to the concerned connected clients. It allows developers to send and receive data without worrying about cross-browser compatibility.

The socket communication relies on the client-server logic, where a persistent connection between a server and a client always exists. To be more precise, the server “opens” a dedicated port where clients get connected to it. Once that happens, all the connected apps can send messages to that port (outgoing messages), and listen to it for any incoming messages. And as that’s the default behavior when connecting to sockets, every connected client to a server will automatically get its messages without any additional request at all. Most importantly, when a message is sent to that port by a server, the recipient clients will instantly receive it, so they can take any further actions (like updating a news feed for example) immediately.

Even though Socket.IO has been designed mostly for web applications, it offers a library for iOS | Android | Java that can be integrated into a project in no-time at all.

Workflow

Firstly, I have created the singleton object of SocketIOManager which will be used by the entire app using the single instance of this class. SocketIOClient need the socketURL as a parameter in our case http://demo.yourdomain.com:3000 .

socket.on(clientEvent: .connect) {data, ack in ...

Emitted when the client connects. This is also called on a successful reconnection. A connect event gets one data item: the namespace that was connected to.

open func emit(_ event: String, with items: [Any])

Send an event to the server, with optional data items.

Sockets created through the manager are retained by the manager. After the successful connection of clients to sockets we send the data to the server using emit method of the socket. So at the very least, a single strong reference to the manager must be maintained to keep sockets alive. In order, to connect to a socket call socket.connect() and to disconnect a socket and remove it from the manager call socket.disconnect() on the socket. So, Let’s define two methods now that will make use of the socket property. The first one connects the app to the server, and the second makes the disconnection.

Now, We are going to establish a connection to the server whenever the app becomes active, and we’ll close that connection when it enters background. Simple enough, so open the AppDelegate.swift file and in the delegate method and establish the connection when applicationDidBecomeActive and close the connection when the applicationDidEnterbackground.

--

--