Socket io implementation in React Native

October 05, 2021 Publish By : EXPERT APP DEVS 3 min read Viewed By : 761
socket io implementation react native

Importance of socket-io

In 21 century, every person needs a chat module in his app. This is a very common functionality so today we are discussing “Socket io implementation in React Native App“.

Why use socket –

- Socket-io is a widely used react-native library.
- Socket-Io is mostly used for applications to get real-time data.
- It enables real-time, two-way, and event-based communication between client and server.
- It is built on JavaScript and Node JS client libraries.
- It has some features-reliability, multi-group room support.
- The endpoint is a combination of IP address and port number.

How Socket-io work –

The server retains the response until it is updated from the backend after receiving it, the client sends another request and requires additional headers that move the additional overhead back and forth.

Makes it possible to open interactive communication between client and server. Anyone can send a request to a server and receive event-based feedback without voting the server for a response, making web sockets the best choice for our use case.

Important Method of Socket-io –

socket.on –

3 different ways to use On Methods

- connect - for connect socket to the server 
Syntax : this.socket.on('connect', () => { });

- event - for handle listener
Syntax : this.socket.on(‘event’, (data) => { });

- disconnect - for disconnect socket to the server
Syntax : this.socket.on('disconnect', () => { });

socket.emit –

After establishing the connection between server and socket by the On method, The emit method is used to send the message between server and client using a particular channel.

Syntax : socket.emit('channel-name', 'message');

Requirements of Socket-io –

1) Rest API backend(server-side).
2) Socket.io front end(app side).

 
Let's start implementation. 

1) Rest API backend(server-side).

We need a backend server that handles all messages and requests from customers, in our case we are going to use the Rest API for the backend, you can use any other such as - Firebase, Laravel, etc.

2) Socket.io front end(app side).

Follow all the steps to implement socket.io

Step 1 – Install the plugin(NPM)

install socket io client plugin
We installed the socket.io-client plugin.

Step 2 – Import the plugin(NPM)

We Import the socket.io-client plugin.

import socket io client plugin

Step 3 – Set State

set state

Step 4 – Initialise the server

We initialise the server in componentDidMount Method.

componentDidMount Method

Define the function connectSocket :

connectSocket function

Step 5 – Design the UI

After successfully setting up the socket, we now design the Send Message view to send the message.

Design the UI

Step 6 - Setup body of sendMessage ()

We define sendingMessages() and integrate the rest API call.

sendingMessages and integrate the rest API call
Here we have completed Socket io implementation in React Native

If you have any queries or issues regarding react native app development, please feel free to ask.

Happy Coding.
 

Need a consultation?

Drop us a line! We are here to answer your questions 24/7.