How to Get Real-Time Location of Vendors When App is Not Running in React Native?

Updated: Feb 05, 2025

How to Get Real-Time Location of Vendors When App is Not Running in React Native?

To get real-time location of vendors when the React Native app is not running, you can use background location services and a server to handle the location updates. Here's a step-by-step guide to implement this solution:

  1. Set up a server:

    • Choose a server technology that suits your needs, such as Node.js, Express, and Socket.io.
    • Create a new project and install the required dependencies.
    • Set up a route to handle location updates.
  2. Implement background location services in React Native:

    • Use a library like react-native-geolocation-service to get the location in the background.
    • Request location permissions and set up a background task to get location updates.
    • Send location updates to the server using HTTP requests or WebSockets.
  3. Implement WebSockets on the server:

    • Install the Socket.io library and set up a new server instance.
    • Create a new route to handle location updates and broadcast them to all connected clients.
  4. Connect the client to the server:

    • Install the Socket.io-client library in the React Native app.
    • Connect to the server and listen for location updates.
  5. Update the UI in the React Native app:

    • Use the received location data to update the UI in real-time.

Here's a sample code structure for the server:

// server.js
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', (socket) => {
  console.log('a user connected');

  socket.on('location', (location) => {
    console.log('location:', location);
    socket.broadcast.emit('location', location);
  });
});

http.listen(3000, () => {
  console.log('listening on *:3000');
});

And here's a sample code structure for the React Native app:

// App.js
import React, { useEffect, useState } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import Geolocation from '@react-native-community/geolocation';
import io from 'socket.io-client';

const App = () => {
  const [location, setLocation] = useState(null);

  useEffect(() => {
    const socket = io('http://localhost:3000');

    Geolocation.requestAuthorization();

    Geolocation.getCurrentPosition(
      (position) => {
        setLocation(position);
        socket.emit('location', position.coords);
      },
      (error) => {
        console.log(error.message);
      },
      { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
    );

    Geolocation.watchPosition(
      (position) => {
        setLocation(position);
        socket.emit('location', position.coords);
      },
      (error) => {
        console.log(error.message);
      },
      { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
    );

    return () => {
      Geolocation.clearWatch();
      socket.disconnect();
    };
  }, []);

  return (
    <View style={styles.container}>
      <Text style={styles.text}>Your location:</Text>
      {location && (
        <Text style={styles.locationText}>
          {location.latitude}, {location.longitude}
        </Text>
      )}
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  text: {
    fontSize: 18,
    marginBottom: 10,
  },
  locationText: {
    fontSize: 24,
    fontWeight: 'bold',
  },
});

export default App;

This is just a basic example to get you started. You may need to modify the code to fit your specific use case, such as handling multiple vendors or securing the connection.