I am using socket.io
in an iOS React Native(v0.20) app
. The app tracks my location, and when my position changes it emits a message to a server. If the socket connection is lost the server sends an email to notify me.
I have the location tracking working in the background with react-native-location, but I can't get socket.io
to work. When ever I change apps or turn off the screen, the app keeps tracking my location but I lose the socket connection.
Is there a way to run socket.io
in the background like location tracking ? Short of that is there some native code that will allow me to maintain a client/server connection
while in the background?
I know there is a WebSocket alternative but I can't see a way to get it to run in the background.
UPDATE:
I doubled checked my Info.plist
, it has the necessary background values set already for react-native-location. I don't know if it matters but the socket work and location tracking are done in the same component.
LocationComponent.js
window.navigator.userAgent = 'react-native';
const io = require('socket.io-client/socket.io');
const socket = io(url, {jsonp: false});
import React, { Text, View, DeviceEventEmitter } from 'react-native';
import { RNLocation } from 'NativeModules';
export default GeolocationExample = React.createClass({
componentDidMount: function() {
RNLocation.requestAlwaysAuthorization();
RNLocation.startUpdatingLocation();
RNLocation.setDistanceFilter(3.0);
DeviceEventEmitter.addListener('locationUpdated', locationObject => {
this.props.newPosition({ longitude: locationObject.coords.longitude, latitude: locationObject.coords.latitude });
});
},
render: function() {
const { lastPosition, distance } = this.props;
socket.emit('newPos', { longitude: lastPosition.longitude, latitude: lastPosition.latitude, distance, time: Date() });
return (
<View>
<Text> {distance} </Text>
</View>
);
}
});