DEV Community

Cover image for Integrating Sockets in Kotlin

Integrating Sockets in Kotlin

jhavidit profile image Vidit Jha Originally published at Medium ・3 min read

Ever wondered how popular apps like Cricbuzz, Whatsapp, Trello, Google Docs work, or thought of building an app that gets automatic live data like a chatting app, a multiplayer game, a live quiz, etc? Then Socket.IO is the answer for you. In this article, I will illustrate how you can implement Socket.IO in your android app.

Socket.IO is a library that enables real-time, bidirectional, and event-based communication between the client and the server.

Socket.IO is built on top of the WebSockets API (Client-side) and NodeJs. But it is not an implementation of web sockets. It is used only for transport. Here both servers and clients can emit and listen to their calls. The server can also trigger the event on a specific route which is listened by their subscribers, which means the client has put their listener for particular events.

Kotlin doesn’t have its official documentation for Socket.IO so we will be studying its operation from Java documentation and will translate it to Kotlin as both languages are interoperable.

There are two methods by which you can implement Socket.IO in your native android app.
Socket.IO Android-Java
Naoyuki Kanezawa Android Socket.IO

The library provided by Naoyuki Kanezawa has also published its blog for Socket.IO using java in android. I would recommend you to use this library as it is easy to implement, provides socket (connect, disconnect, reconnect) detection, auto-reconnect feature, and is also promoted in official Socket.IO documentation.

Now let’s move to the coding part and in the next few steps, you can set up your connection. ✌️


• First and foremost add the dependency to app-level build.gradle file.

• Don’t forget to add internet permission in the manifest file. 😅

Socket Initialization😎

• Create a Kotlin class for socket initialization which can be used anywhere in the project. Here IO.socket() method returns a socket for with the default options. Notice that the method mSocket caches the result, so you can always get the same Socket instance for an URL from any Activity or Fragment.

• Don’t forget to add your socket initialization application class name in the manifest file. 😅

Socket Connection to the Server😃

• Now we will create the object of the Socket Instance class which we created earlier and connect the socket using .connect() method.
We will use IO.options() method so that socket will be reconnected by itself if ever It gets disconnected due to some network problem.

• To check if the socket is connected or not we can use .connected() method.

Emit and Listen Events😊

• For receiving instant real-time data we need to use on() method. We can get data in a JSON data block or any other data form. To show anything on the screen all the UI bind logic should be coded on the UI Thread part as the socket works on the background thread.

• Checking when the socket is disconnected or reconnected.

• For sending data to the server in the form of JSON data block or any other data form we have to use the emit() method.

If you are going to receive data based on what you emit to the server then first you should start listening to the event using the on() method and then emit your data.

• And last but not the least, don’t forget to close the socket connection using .disconnect() method and remove the listener using .off() method when you are done.😇

Let me know in the comments if you have any issues and topics you would be interested in learning more about. You can also reach out to me on Twitter and Instagram.

Discussion (2)

Editor guide
robcam3l profile image

Hey, this example does not seem to work for me on multiple levels.

Primarily within mainActivity m.socket is unresolved, once i make my way around that, thew main activity then does not know how to find io from IO.Options.

I have followed your example exactly as you have written it, i have tried to work my way around it, and regardless of what i do, i am not able to make an instance of an android app connect to a server.

Am i missing something?

drewlylooly profile image

One thing that isn't really shown here is giving your app permissions to use the internet and access the network state in the Manifest - that would be a good thing to try if you're still having issues