A native implementation of Socket.io for React Native.
io.socket:socket.io-client:1.0.1
which supports 2.x (or 3.1.x / 4.x with allowEIO3: true)
as described at the compatibility page.
After experiencing some performance problems with the JS socket.io library in React Native context, I decided to write a module using the Java solution that can be used in the UI/background threads and by doing so, keeping the JS thread more open to others expensive works and having the performance 💯, and a more stable development experience.
npm install react-native-socket-io-wrapper
In MainApplication.java add the following:
@Override
protected List<ReactPackage> getPackages() {
return Arrays.asList(
new SocketIoTurboPackage(), // <-- add
new MainReactPackage()
);
}
import SocketIO from "react-native-socket-io-wrapper";
// ...
const socketIO = new SocketIO('http://127.0.0.1:3000', {
transports: ['websocket'],
query: SocketIO.serializeQuery({
token: 'Bearer JWT',
}),
});
socketIO.connect();
Open socket connection.
socketIO.disconnect();
Close socket connection.
socketIO.on(eventName, callback);
Listen to socket event.
socketIO.once(eventName, callback);
Listen once to socket event.
eventName: string
callback: Function
socketIO.emit(eventName, data);
Send socket event.
socketIO.off(eventName, data);
Remove socket event listener.
eventName: string
data: any
socketIO.connected(callback);
Get connection status of socket.
socketIO.connectedSync();
Get connection status of socket.
socketIO.getId(callback);
Get id of socket.
socketIO.getIdSync();
Get id of socket.
socketIO.updateSocketOptions(updatedOptions);
Update socket options, this updates general instances paths.
socketIO.updateSocketOptionsSync(updatedOptions);
Update socket options, this updates general instances paths.
- Write tests
- Implement iOS native module
See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT