From some of the latest stack over flow questions, navigator.userAgent object is changed into react-native and then importing library, but this is very old and good method. I honestly couldn’t find a good article to connect socket.io with react-native.
The point of this article isn’t explain developing any react-native app with socket.io, but to demonstrate how to emit and listens to sockets in RN.
If you didn’t installed client side socket.io package on your RN app, you can easily done by running following command.
npm install --save socket.io-client
Now, go back to your app and import SocketIOClient
import SocketIOClient from ‘socket.io-client'
In your constructor, create an instance which will connect with your backend.
this.socket = SocketIOClient('http://localhost:80000');
Now, you’re automatically connected with your server. And you can start emitting to server & listens to data from server.
In the following example, Constructor will create an instance to socket of type SocketIOClient , and automatically connects with your server. When your app loads, socket emits a message ‘Hi server’ to your server through ‘channel1’.
When a user hit on ‘Click’ button, socket.io will emit an object to your server.
Don’t forget to install dependencies and socket.io server library.
npm install --save socket.io