react-nativepubnub

PubNubReact not working as it is deprecated


I'm trying to use PubNub in React Native to implement real time location tracking, but since the code I was referring to is 4 years old, PubNubReact isnt there anymore. I dont know how to use the current available version. I have used react native maps and google maps api for this.

Here's the code for reference:

import React from 'react';
import { StyleSheet, View, Platform, Dimensions, SafeAreaView } from 'react-native';
import MapView, {Marker, AnimatedRegion} from 'react-native-maps';
import PubNubReact from 'pubnub-react'

const { width, height } = Dimensions.get("window");

const ASPECT_RATIO = width / height;
const LATITUDE = 37.78825;
const LONGITUDE = -122.4324;
const LATITUDE_DELTA = 0.0922;
const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO;

export default class App extends React.Component{
  constructor(props){
    super(props);
    this.state = {
      latitude: LATITUDE,
      longitude: LONGITUDE,
      coordinate: new AnimatedRegion({
        latitude: LATITUDE,
        longitude: LONGITUDE,
        latitudeDelta: 0,
        longitudeDelta: 0
      })
    }
    this.pubnub = new PubNubReact({
      publishKey: "pubKey",
      subscribeKey: "subKey",
    });
    this.pubnub.init(this);
  }
 ...

Solution

  • Oliver from the DevRel team at PubNub. Our React Native SDK was deprecated some time back, but I would recommend using our React SDK in your mobile app. It essentially is a wrapper around our JavaScript SDK to use JS code in your app.

    Once you've added the PubNub JavaScript SDK and React framework packages to your project

    cd <project folder name here>
    npm install --save pubnub pubnub-react
    

    You'll need to include the PubNub libraries and configure the PubNub object with a uuid, or Unique User Identifier. You should be then good to go!

    import PubNub from 'pubnub';
    import { PubNubProvider, usePubNub } from 'pubnub-react';
    ...
    const pubnub = new PubNub({
          publishKey: 'myPublishKey',
          subscribeKey: 'mySubscribeKey',
          uuid: 'myUniqueUUID'
        });
    ...
    

    With those adjustments in mind, follow some example code of not only initializing some sample code, but as well as how to add event listeners to capture events on the PubNub platform.