Is it possible to run graphql subscriptions on firebase? - firebase

I want to use GraphQL subscription with Firebase cloud functions.
In my previous question I described that when I deployed my code to Firebase it threw me "Could not connect to websocket endpoint." and I couldn't even connect to it manually,but when running locally everything works like a charm.
After that I went to see if it is possible to run subscriptions with Firebase,but didn't find anything,only for queries and mutations(it works for me too).
The only post I have found is this:
https://www.reddit.com/r/graphql/comments/aijebt/graphql_on_firebase/
where someone wrote this :
"No support for GraphQL subscriptions. GraphQL subscriptions rely on a
persistent web socket connection. Since functions shutdown during
inactivity, persistent web socket connections are impossible to
support."
However I don't want to give up here,because it was 6 months ago,and he said it he tried it while it was beta,so I'm hoping it have the support now.
So my question is:
Is it possible to run subscriptions on Firebase?
If not what are your
advise to do real-time stuff on back-end with Firebase?
Thanks!

Accordingly to some posts firebase-functions has a maximum lifetime of 9 minutes,so the subscriptions won't work.

Related

Firebase Emulator Cloud Functions + PubSub subscribe to production topics

There is a service that is publishing messages to my Pub/Sub. Via CLI, I know it is receiving properly the messages.
I want to react correspondingly to those messages. However, I want to develop my subscription, via Cloud Functions, in development environment (firebase emulator), so I won't have to wait 5min between each deploy. But, when using functions.pubsub.topic('topicName').onPublish(...), it won't subscribe to the real prod messages, looks like it will only subscribe to the dev env ones.
I want to, in my firebase emulated Sub/Pub, subscribe to prod messages. Is it possible to do it? How?
Still haven't found an "official" way.
What I am doing for now is use ngrok, get the local function url and then enter it in Pub/Sub Subscription in Push mode. It's a longer way and will require updating the ngrok url for each session (as its url changes in free tier), and also to get the data, JSON.parse(Buffer.from(req.body.message.data, 'base64').toString('utf-8')) and still haven't found a way to auth the JWT auth from request.
But, I can now get the Prod messages from my Firebase Emulator, as I want. You may comment here to ask for further infos about that workaround of mine.

How do I access/create topics on firebase pubsub emulator?

I've added/setup the firebase pubsub emulator that is running on my localhost on port 8085. Up till this point I had need to use the pubsub in Googel cloud, where I would use their console to create topics and could also review messages published to those topics.
Now that it's running locally, there doesn't seem to be a UI to manage topics/messages and I can't seem to find documentation that explains how to create a topic on the local pubsub, how to review existing topics and how to review messages?
In addition, if I am writing a cloud function that references the pubsub library, will it automatically use my local emulator version if it's running or do I need to specify the host/port in my node.js function someplace? Any examples of this out there?
I believe their is an http API I Can use to create topics,but again, I can't find any documentation on this or find references to see what topics exist and what messages have been sent to the topics.
Any help is appreciated.
In order to connect your app to the local emulator you must set some environment variables like explained here
About the UI of the emulator you can access it on http://localhost:4000 like this documentation page mentions, although I am not sure if there are management tools to manage topics or messages in there.

Why does Firebase Realtime db work in incognito, etc but not Firebase Cloud Messaging

I'm just getting started with the Firebase ecosystem and doing some investigating before committing to a GCloud database implementation (Datastore vs. Firestore). My target client is Web/browser and my backend services will be in AppEngine.
Tinkering with the cloud messaging quickstart example, it seems that FCM doesn't work in several scenarios (incognito mode, Safari, if permission blocked). However, the friendly chat demo that uses realtime db updates does appear to work in these scenarios.
High level could anyone explain why?
My goal is to subscribe to events for the client to update state, etc without the use of polling. It seems overkill to put these into firestore just to bypass the notifications permission requirements. Any insight is appreciated. These events could be dispatched from several different backend services and are not exclusively bound to db records.
Update
For what I wanted to do, I was able to simply use Firestore and had no needs that targeted specific devices that merited using FCM. Firestore came out of beta since I posted this question and works well for live subscribing to queries that update local state, which is ultimately what I needed to do.
The FCM client works on the promise that it can identify your browser, even when the page is not open. So it needs to persistently be able to identify the browser. It uses an Instance Token for that, which I assume it persists in the local storage of your browser. And since an incognito window has its own local storage, it becomes a separate instance ID.
The Firebase Realtime Database itself does not persist any information about the instance. Instead you pass in all the necessary information when you initialize the FirebaseApp instance, and then get a DatabaseReference. Since the incognito window runs the same code, it's accessing the same information in the database, and thus seeing the same result.
Note that this would be different if you use Firebase Authentication in combination with the Realtime Database. Auth will persist the user token to local storage, so that won't be shared with the incognito window.

Can Firebase be used in an app supported by another Backend and Database?

I am looking to make an app that would have its Backend on another service like AWS or some other. This app would be having many features and functionalities.
But for chat feature, I am exploring options and wondering that would I be able to integrate Firebase in my app.
I have read about Firebase Functions to add more functionality at the backend and also the installation of Firebase Admin to servers.
But still I am not convinced about their capabilities and exactly what all I can do with them.
It would be great if someone who has experience with Firebase help me out figuring if going with it is the best case for me or is there something else I should look into.
So first you can't use Firebase in combination with AWS or Azure etc. Firebase is based on Google Cloud and is the interface between the mobile client (the running app on the client's smartphone) and the backend (your Firebase project).
What I use is, for example, Firebase Cloud Messaging, to simply notify one or multiple users by trigger an HTTP Request from my own web server.
I also made some apps to store the data in FireStore or in the Realtime-Database, so that I don't have to set up a whole new infrastructure. And this is basically the goal of Firebase that you can simply start with your app, without carrying about that.
So what I've heard about Firebase is that you currently cannot install Firebase on a server of your choice and you have to use Google Cloud.
Hopefully, you can do something with my answer. If you have further questions feel free to ask them.

How do I communicate / trigger a Webtask from Firebas?

In an interesting blogpost about 'Firebase Authentication with the Firebase 3.0 SDK and Auth0 Integration', it is stated that:
You can even have Firebase communicate with Webtask!
Now I can imagine the (web)client triggering a Firebase operation and subsequently a Webtask, but not the other way around. Or am I missing something?
Firebase can run as a serverless app, but it can also run on the server. You can even have Firebase communicate with Webtask! (sic!)
I think that paragraph is misleadingly phrased, perhaps it was just added at the last minute to spark interest. You can have a webtask communicate with Firebase, not the other way around. You don't "run Firebase" on your server either.
TL;DR: A client application may call a webtask with an HTTP request, and that task can read/write the database, but not in any other order.
Here's a quick and dirty reality check as of Nov. 2016:
The Realtime Database by itself does not provide you with a way of executing code. This includes responding to database changes and user requests, handling fan-in and fan-out operations, etc. There is no support for webhooks either.
Which means you have to provide your own execution environment for such logic on a custom server, or you can try to cram as much as possible into the client code. This is a pretty exhaustive topic by itself.
Webtasks are short-lived functions that respond to HTTP requests. Their lifecycle always starts with a request, so they are not fit for continuously watching the database for changes. But they are perfectly valid for handling requests coming in from your client application.
As you can store "secrets" for the webtasks, you can authenticate the task on an admin access level. This gives you the possibility to verify client tokens – which should be sent along with the request –; perform complex authorization and validation, and perform RTDB write operations you wouldn't trust the clients with.
Or trigger external services securely. The possibilities are close to endless.

Resources