A Quick guide to Socket IO Communication

Socket.IO

Socket.IO is a JavaScript library for realtime internet applications. It permits real time, bidirectional communication between internet shoppers and servers. It has 2 parts: a client-side library that runs within the browser, and a server-side library for node.js.

It provides more options, as well as broadcasting to multiple sockets, storing information related to every client, and asynchronous I/O.

Socket.IO Features

Socket.IO provides the flexibility to implement time period analytics, binary streaming, instant electronic communication, and document collaboration.

Socket.IO handles the connection transparently. It will automatically upgrade to WebSocket if possible. This requires the programmer to only have Socket.IO knowledge.

Introduction :

The application is based on chatting using Scoket.IO .The application code will help you to use Socket.IO in your android application.

The application provides below functionalities :-

1) Sending Messages to all users.

2) Notify when the user leaves or joins the room.

3) Indicates if the user has started typing.

Adding the Dependencies :

First you need to install the Socket.IO with gradle.

Add the below line in your build.gradle file :-

Permissions Needed :

You must give the internet permission to AndroidManifest.xml.

How to use socket in Activity and Fragment :

First, you need to create an object of the Socket object.

Using socket.connect() we are going to connect through server. Different type of listener is provided by the Socket.IO and we can use that using socket.on() method like above.

socket.on() method attach the listener which automatically called.

Socket.EVENT_CONNECT – Call when the connection is successfully created.
Socket.EVENT_DISCONNECT – Call when the connection is terminated.
Socket.EVENT_CONNECT_ERROR – Call when any error occur.
Socket.EVENT_CONNECT_TIMEOUT – Call if timeout occur.

You can also add custom listener like getActiveUser to get all active user and addMeToRoom to add in specific room.

Emitting events :

socket.emit() is used to send the message.

Listener:

Create the listener to get different events callback and perform task accordingly.

onDisconnect call if the connection is terminated.

onConnectError call if any error occur.

onConnect call if the connection is successfully created.

I hope you find my article useful to implement Socket IO in your Android Application. Suggestions are welcomed, please do comment below.

Mahavir Nahta

Mahavir Nahta | Android Developer

I am an Android Developer at Yudiz Solutions Pvt. Ltd. - a Mobile App Development company. I am passionate about developing android apps and like to work with real-time data communication with hardware connectivity, to maximize development efficiency and produce innovative applications.
mm

Mahavir Nahta Android Developer

I am an Android Developer at Yudiz Solutions Pvt. Ltd. - a Mobile App Development company. I am passionate about developing android apps and like to work with real-time data communication with hardware connectivity, to maximize development efficiency and produce innovative applications.

Comments are closed.

Top