Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase client on ReactNative

When using Firebase on ReactNative, it will show such error message:

can't find variable process

However, if I require firebase/lib/firebase-web.js manually, it will show:

can't find variable document

How can I resolve this?

like image 211
lazywei Avatar asked Apr 08 '15 14:04

lazywei


People also ask

Can I use Firebase with React Native?

Firebase is a Backend as a Service (BaaS) that provides an advantage to mobile developers who use React Native for developing mobile applications. As a React Native developer, by using Firebase you can start building an MVP (minimum viable product), keeping the costs low and prototyping the application pretty fast.

How do I fetch data from Firebase in React Native?

let users = firebase. database(). ref('users/' + userId) , you can read the data either once using the . once() method or add a listener for changes in the data using on() .

Can I use React Native Firebase with Expo?

Next steps After configuring React Native Firebase library, you can use any module it provides in your Expo project. For more information to install and use a certain module from React Native Firebase, we recommend you to check their documentation.


2 Answers

I just went through the same issue while trying to use sockets.io in my react native app so hopefully I can help.

The reason that you cannot use firebase's node module is because there hasn't been a polyfill created yet for websockets support (which firebase is dependent on) in react native.

If you take a look at issue #619 in react native's repo you'll find the current discussion on creating a websockets api polyfill.

The way that we solved it is by using Jason's modified version of the sockets library and creating our own repo around just that file. Then we added the line below to our package.json dependencies.

"react-sockets": "crewapp/react-native-sockets-io"

enter image description here

The reason that Jason's version of the sockets.io client file works is because react-native is added as a user agent. You can find the code that makes this change at the top of the file:

window.navigator = {
  userAgent: "react-native"
}

Once you've gone through these steps you should be able to require sockets.io / firebase as normal.

like image 90
Pavan Ravipati Avatar answered Oct 04 '22 16:10

Pavan Ravipati


Just figuring it our. Pavan's answer is helpful, but it is not quite true when using with Firebase.

For firebase, please follow the steps:

  1. Download the firebase-debug.js from wsExample. Or you can just install wsExample by npm and require the firebase-debug.js inside it.
  2. Use badfortrains's forked React-Native:

    "react-native": "git://github.com/badfortrains/react-native#WebSocket"
    
  3. New the Firebase like this:

    var firebase = require("../../firebase-debug.js");
    var rootRef = new Firebase(Const.FB_ROOT);
    

Things should just work now!

like image 30
lazywei Avatar answered Oct 04 '22 15:10

lazywei