I am trying to use Firebase SDK on my Google App Script app.
The documentation says it needs installing npm install firebase which is not possible via Google App Script.
So the following step would be:
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "***",
authDomain: "myapp.firebaseapp.com",
databaseURL: "https://myapp-default-rtdb.europe-west1.firebasedatabase.app",
projectId: "myappurl",
storageBucket: "myappurl.appspot.com",
messagingSenderId: "267227785525",
appId: "***",
measurementId: "G-K5CP51FRXR"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
But i get the error:
SyntaxError: Cannot use import statement outside a module linea:
It looks like i am not declaring it as a module.
I have checked for tutorials and help but only find the how-to for Node.js, Google Script is not talked about.
Note: I have installed 'FirebaseApp' library.
Anyone knows how to do what i need? Is that even possible to use SDK v9 on GAS?
While Google Apps Script and Node.js uses JavaScript as programming language, Node.js has several features that aren't included in Google Apps Script, one of them is the the npm packages handling.
One option might be to use the npm package "as a client-side library". Please start by reading https://developers.google.com/apps-script/guides/web, then checkout Use npm package on client side.
Some client-side libraries might work as server side JavaScript. Nowadays Google Apps Script has two runtimes, but one, Rhino, depends on the old Google Apps Script IDE that will be retired soon. To learn about the new runtime, V8, please read https://developers.google.com/apps-script/guides/v8-runtime.
Some npm packages have being "converted" into Google Apps Script libraries. You might try to find on by searching this site, a service like GitHub, etc. Another option is to ask for a software recommendation on https://softwarerecs.stackexchange.com.
Another option is to use the Firebase REST API directly in Google Apps Script by using the Url Fetch Service.
Related
Using an imported module inside Google App Script
How to integrate Firebase into Google Apps Script without using (deprecated) database secret
Cannot add Firebase library to Apps Script
Syntax error: SyntaxError: Cannot use import statement outside a module line: 1 file: Code.gs
Related
What for should I link the web app to the Firebase Hosting site in the console? What are the benefits and effects?
The difference between the two is covered here: Whats the difference between firebase web app and firebase hosting.
The linking is mentioned in the documentation https://firebase.google.com/docs/web/setup#register-app, but the rationale and consequences are not explained.
The Firebase support documentation says:
You may need to link your web app to your Hosting site in the Project settings if you chose not to set up Hosting when you registered your web app with your Firebase project.
If you're using Firebase Hosting for your web app, you can use the reserved Hosting URLs to add Firebase SDKs and manage your Firebase configuration for your app.
In short, if you're using those special Hosting URLs that begin with __, in particular, /__/firebase/init.json, it helps to have linked your app to Hosting so that the configuration is complete. By my recollection, the config will then have a correct FCM config in this case.
You can configure partial import of the Firebase JavaScript SDK and only load the Firebase products that you need. If you're using a bundler (like Browserify or webpack), you can import individual Firebase products when you need them.
Note: You can use this option even if you use Firebase Hosting, but make sure to add your Firebase config object before initializing Firebase.
Install the Firebase JavaScript SDK:
a. If you don't already have a package.json file, create one by running the following command from the root of your JavaScript project:
npm init
b. Install the firebase npm package and save it to your package.json file by running:
npm install --save firebase
To include only specific Firebase products (like Authentication and Cloud Firestore), import Firebase modules:
// Firebase App (the core Firebase SDK) is always required and must be
listed first
import firebase from "firebase/app";
// If you are using v7 or any earlier version of the JS SDK, you should
import firebase using namespace import
// import * as firebase from "firebase/app"
// If you enabled Analytics in your project, add the Firebase SDK for
Analytics
import "firebase/analytics";
// Add the Firebase products that you want to use
import "firebase/auth";
import "firebase/firestore";
Initialize Firebase in your app:
// TODO: Replace the following with your app's Firebase project
configuration
// For Firebase JavaScript SDK v7.20.0 and later, measurementId is an
optional field
const firebaseConfig = {
// ...
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
I'm developing an app with react-native (v0.60) and I need push notifications. I decided to use firebase as service but I have an error during initialisation.
Code:
import {AppRegistry} from 'react-native';
import Config from 'react-native-config'
import * as firebase from 'firebase/app'
import '#firebase/messaging'
import firebaseConfig from 'App/src/firebase.config'
import App from './App';
import {name as appName} from './app.json';
firebase.initializeApp(firebaseConfig)
const messaging = firebase.messaging()
messaging.usePublicVapidKey(Config.FIREBASE_KEY_PAIR)
AppRegistry.registerComponent(appName, () => App);
The problem is that I get the error:
ReferenceError: Can't find variable: IDBIndex
How can I fix this?
firebase only supports Authentication, Firestore & Realtime databases, Storage, and Functions on React Native. Other modules like Analytics are not supported through the Firebase JS SDK, but you can use expo-firebase-analytics for that. If you'd like access to the full suite of native firebase tools
please remove firebase.analytics();
It's because you have enabled Google Analytics for your app. Disable Analytics from the Firebase settings and again copy the Firebase file data from Firebase and replace it with the old firebase.js file in the project.
I would advise to use the React Native Firebase wrapper instead. As they say:
Although some features from the Firebase Web SDK will generally work with React Native, it is mainly built for the web and as such has a limited compatible feature set. In contrast we use the Firebase Native SDKs - this allows us to provide APIs to a vast majority of Firebase products and services.
please remove firebase.analytics(); form where ?
I have an application build on Laravel.
Purpose of that application is to perform deliveries of goods.
It involves real-time tracking.
On the other hand I am building same application on Angular & Meteor.
Meanwhile I want to store the tracking data in Meteor database (mongodb).
Is there anyway to access Meteor server into my Laravel application to show the real-time tracking?
UPDATE
I tried Paulo Mogollón solution but I am facing this error:
Loading failed for the module with source file:///E:/Dev/laravelapp/node_modules/isomorphic-ws/index.d.ts
jsfiddle.net/Lrxjed8v
You can connect to your meteor server from anywhere using js with the simpleddp package, basically you do this.
npm install simpleddp isomorphic-ws --save
import ws from 'isomorphic-ws';
import simpleDDP from 'simpleDDP'; // ES6
const opts = {
endpoint: "ws://someserver.com/websocket",
SocketConstructor: ws,
reconnectInterval: 5000
};
const server = new simpleDDP(opts);
let userSub = server.subscribe("user_pub");
let otherSub = server.subscribe("other_pub", 'param1', 2); // you can specify arguments for subscription
(async ()=>{
await userSub.ready();
let nextSub = server.subscribe("next_pub"); // subscribing after sub is ready
await nextSub.ready();
//all subs are ready here
})();
You can find more examples and information in the package repo. Hope this takes you in the right direction.
Check that you have installed both modules npm install simpleddp isomorphic-ws --save.
Btw do you have node.js and npm installed? You should also configure you build manager like webpack. Or you can just download minified pre-compiled versions of these libs:
https://www.jsdelivr.com/package/npm/simpleddp
https://www.jsdelivr.com/package/npm/isomorphic-ws
Just loaded firebase 3.0 with react-native and getting the error
[fatal][tid:com.facebook.react.RCTExceptionsManagerQueue] Unhandled
JS Exception: Can't find variable: document
Is react-native supported yet?
React-native code:
var firebase = require('firebase')
// Initialize Firebase
var config = {
apiKey: '<apiKey>',
authDomain: '<app>.firebaseapp.com',
databaseURL: 'https://<app>.firebaseio.com',
storageBucket: 'firebase-<app>.appspot.com'
};
firebase.config(config)
The newest version of firebase uses the document variables that are used in web applications and the RCTWebSocket or RCTView doesn't work the same way. (I'm not 100% sure about all the details). Anyway, you can just install an older version of firebase and it will work for you. Assuming you have npm installed follow these instructions.
Uninstall Firebase from your react-native project npm uninstall firebase --save
Install version 2.4.2 of firebase npm install firebase#2.4.2 --save
Happy Firebaseing!
Firebase JS SDK 3.1 just got released and is now compatible with React Native!
See release notes.
I was able to get Firebase 3.0.2 to load in React Native by putting the following in a .js file and importing it at the top of index.ios.js:
global.location = {
href: ''
};
global.screen = {
};
global.document = {
getElementsByTagName: function(){}
};
global.parent = global;
There may be other gotchas - I have not explored further than this but it might get Firebase 3 working for you.
Firebase 3.x is not supported in RN (yet..).
Issue
I think the issue relates to the auth module's dependency on the browser's window variable.
Will it be officially supported?
According to the following discussion thread from the Firebase team, they are presently working on 3.x support. There is however no release time frame set.
Google Groups Discussion.
Workarounds
Option 1 (with FB 2.x)
From Jacob Wenger:
If React Native support is a blocker for you, please continue to use
the 2.x.x SDKs until we resolve this issue. The 2.x.x SDKs will
continue to work, even after you migrate to the new console..`
Option 2 (with FB 3.x)
From Jacob Wenger:
As a "workaround" if you don't need auth (unlikely, I know, but still
worth mentioning), you should be able to do the following:
var app = require('firebase/app');
var database = require('firebase/database');
To use last version of Firebase with ReactNative you can use Firebase Bridge.
I have a demo app here.
Whilst the Firebase JS SDK does work on react native now, it is mainly built for the web and is generally not the best solution for react-native.
The Firebase Web SDK runs entirely on react native's JS thread, therefore affecting your application frame rate (the link explains this well).
In my tests, the native firebase SDK's has been roughly 2-3 times quicker than using the web SDK.
But on top of the potential performance impacts there's a lot of features you'll be unable to use with the web SDK on android/ios devices. For example:
Notifications / FCM
Offline capabilities
Storage upload/download
Firebase Crash Reporting
Analytics
Use of social authentication providers
The best approach would be to run with the native android/ios firebase sdk's and have a bridge between them and your js code (i.e. a native module setup).
Thankfully you don't have to implement this yourself, there's already modules out there to do this for you:
react-native-firebase for example mirrors the the web sdk's api js side but executes on the native side using the native android & ios firebase sdk's. It's fully compatible with any existing firebase js logic that you may have already implemented and is intended as a drop in replacement for the web sdk.
(disclaimer: I am the author of react-native-firebase)
Just loaded firebase 3.0 with react-native and getting the error
[fatal][tid:com.facebook.react.RCTExceptionsManagerQueue] Unhandled
JS Exception: Can't find variable: document
Is react-native supported yet?
React-native code:
var firebase = require('firebase')
// Initialize Firebase
var config = {
apiKey: '<apiKey>',
authDomain: '<app>.firebaseapp.com',
databaseURL: 'https://<app>.firebaseio.com',
storageBucket: 'firebase-<app>.appspot.com'
};
firebase.config(config)
The newest version of firebase uses the document variables that are used in web applications and the RCTWebSocket or RCTView doesn't work the same way. (I'm not 100% sure about all the details). Anyway, you can just install an older version of firebase and it will work for you. Assuming you have npm installed follow these instructions.
Uninstall Firebase from your react-native project npm uninstall firebase --save
Install version 2.4.2 of firebase npm install firebase#2.4.2 --save
Happy Firebaseing!
Firebase JS SDK 3.1 just got released and is now compatible with React Native!
See release notes.
I was able to get Firebase 3.0.2 to load in React Native by putting the following in a .js file and importing it at the top of index.ios.js:
global.location = {
href: ''
};
global.screen = {
};
global.document = {
getElementsByTagName: function(){}
};
global.parent = global;
There may be other gotchas - I have not explored further than this but it might get Firebase 3 working for you.
Firebase 3.x is not supported in RN (yet..).
Issue
I think the issue relates to the auth module's dependency on the browser's window variable.
Will it be officially supported?
According to the following discussion thread from the Firebase team, they are presently working on 3.x support. There is however no release time frame set.
Google Groups Discussion.
Workarounds
Option 1 (with FB 2.x)
From Jacob Wenger:
If React Native support is a blocker for you, please continue to use
the 2.x.x SDKs until we resolve this issue. The 2.x.x SDKs will
continue to work, even after you migrate to the new console..`
Option 2 (with FB 3.x)
From Jacob Wenger:
As a "workaround" if you don't need auth (unlikely, I know, but still
worth mentioning), you should be able to do the following:
var app = require('firebase/app');
var database = require('firebase/database');
To use last version of Firebase with ReactNative you can use Firebase Bridge.
I have a demo app here.
Whilst the Firebase JS SDK does work on react native now, it is mainly built for the web and is generally not the best solution for react-native.
The Firebase Web SDK runs entirely on react native's JS thread, therefore affecting your application frame rate (the link explains this well).
In my tests, the native firebase SDK's has been roughly 2-3 times quicker than using the web SDK.
But on top of the potential performance impacts there's a lot of features you'll be unable to use with the web SDK on android/ios devices. For example:
Notifications / FCM
Offline capabilities
Storage upload/download
Firebase Crash Reporting
Analytics
Use of social authentication providers
The best approach would be to run with the native android/ios firebase sdk's and have a bridge between them and your js code (i.e. a native module setup).
Thankfully you don't have to implement this yourself, there's already modules out there to do this for you:
react-native-firebase for example mirrors the the web sdk's api js side but executes on the native side using the native android & ios firebase sdk's. It's fully compatible with any existing firebase js logic that you may have already implemented and is intended as a drop in replacement for the web sdk.
(disclaimer: I am the author of react-native-firebase)