React Native With Socket.io Client

React Native With Socket.io Client

Install socket.io-client library

Copy and run following command at project’s root folder.

1
npm i socket.io-client --save

How to use

Import new library have installed to project

You need to import the correct path as below for this library to work.

1
import io from 'socket.io-client/dist/socket.io';

Connect to socket server and listen event

You can make an connect to socket server in constructor function.

1
2
3
4
5
6
7
constructor(props) {
super(props);
this.socket = io('http://yoursocketserver', {jsonp: false});
this.socket.on('event', function (data) {
// writing your code to handle data here
});
}

Function this.socket.on() to listen an event from socket server

Send an event and data to socket server

To call an event to socket server you can using emit function.

1
this.socket.emit('event', data);

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×