4

I am using the firebase emulator to host some GCF functions on my machine. They are configured to run/host on localhost:5001. This works great.

I am now using Google Tasks in my app, and the task I have needs to call a GCF function. Tasks does not run locally, so I've setup my machine to be accessed via a public IP address and opened the port 5001 to allow traffic in on that port. The idea is that the Google task can call the function on my machine, so I can properly test.

I cannot seem to get the emulator to work with any outside public IP address. Is this just not possible or if it is, how do I configure?

UPDATED, there isn't much code to review here...I am just wanting to know if you can configure the emulator to listen on a public port. Here is the default firebase.json file

   {
     "functions": {
        "predeploy": [
         "npm --prefix \"$RESOURCE_DIR\" run lint"
    ],
    "source": "functions"
  },
  "emulators": {
    "functions": {
      "port": 5001
    },
    "pubsub": {
      "port": 8085
    },
    "ui": {
      "enabled": true
    }
  }
}

In my research, I found you can add the "host" attribute to allow your local network to access the emulator using "host": "0.0.0.0", so it looks like this: This works to allow access via my local ip like http://192.168.1.216:5001

{
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint"
    ],
    "source": "functions"
  },
  "emulators": {
    "functions": {
      "port": 5001,
      "host": "0.0.0.0"
    },
    "pubsub": {
      "port": 8085
    },
    "ui": {
      "enabled": true
    }
  }
}

Now, using my public ip, which is setup using NAT FORWARDING, it can't access the site http://108.49.78.181:5001

there isn't code so much as, is this even possible? If it is, I'd love some example of how to do it.

mike hennessy
  • 1,359
  • 1
  • 16
  • 35
  • I think you're going to have to provide the code and configuration that's not working the way you expect, along with any logs and error messages. You could be doing any number of things incorrectly, and we can't see any of that. There should be enough information in your question that anyone can reproduce the issue. – Doug Stevenson Jul 23 '20 at 16:08
  • I put some more in my update, but this isn't really a code issue, it's a cofigutration question of whether you can get the firebase emulator to listen to a public ip. Is it possible? – mike hennessy Jul 23 '20 at 16:18
  • You can test that by simply trying to access the exposed IP and port and seeing if it connects. I strongly doubt the emulator is coded to categorically deny requests coming from outside the localhost. But you can see all the code for yourself on GitHub. https://github.com/firebase/firebase-tools – Doug Stevenson Jul 23 '20 at 16:25
  • So, if you don't specify the "host" attribute, you can't connect to the service on your local computer such as http://192.168.1.216:5001 . You get "Site cannot be reached"...you can only get to it via http://localhost. If you add the "host":"0.0.0.0", you can now listens via the 192.168.1.216:5001, but it doesn't respond to http://108.49.78.181:5001 . I should note, I have tested accessing my webserver with exact NAT forwarding (http://108.49.78.181:80) and it works. So the question is how to configure host to allow public ip? I tried using public ip here and it does't even start. – mike hennessy Jul 23 '20 at 16:48
  • No idea. If the configuration isn't working the you expect, you could file an issue on GitHub. – Doug Stevenson Jul 23 '20 at 16:52
  • I'll try that...it may just not be supported. – mike hennessy Jul 23 '20 at 16:53
  • [Todd](https://stackoverflow.com/users/14748620) posted an [Answer](https://stackoverflow.com/a/65107230) saying "Have you tried cross origin allow config for public access?" I assume it was a rhetorical question. – Scratte Dec 02 '20 at 14:37
  • Any update on that? I am using a system that calls via webhook my function via http request. Works for deployed functions, but as deploying takes 2 min, I want to the webhook call my ip and test and develop it locally – Henrique Bruno Dec 09 '20 at 18:13

2 Answers2

6

Accessing the function should be like this

http://localhost:5001/{project_name}/us-central1/{function_name}

{project_name} must be replaced by the name of your project in .firebaserc
{function_name} must be replaced by the name of the function exported from your js file

Requesting http://localhost:5001/ directly should return Not Found

Access from external IP

Using host with "0.0.0.0" means that the server will listen to all ip addresses.

By default firebase accept only localhost but you can change this by using the host option in firebase.json for each specific emulators.

"hosting": {
   "port": 5000,
   "host": "0.0.0.0"
},
"functions": {
   "port": 5001,
   "host": "0.0.0.0"
}

and others like firestore,auth,ui,database...

Start emulators with

 firebase emulators:start

Don't forget to port forward your router to the local ip of your computer running the emulator and accept port in your firewall

user2258152
  • 635
  • 5
  • 13
  • 1
    Thanks for the tip on using `0.0.0.0`. If anyone else is interested, take a look at https://stackoverflow.com/questions/20778771/what-is-the-difference-between-0-0-0-0-127-0-0-1-and-localhost – Ben Butterworth Nov 18 '21 at 23:53
1

Yes You can access this from public IP address or private IP address within private network. All you need to do is change the host from firebase.json file.

{
"firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "emulators": {
    "functions": {
      "host": "192.168.0.240",
      "port": 5001
    },
    "firestore": {
      "host": "192.168.0.240",
      "port": 8080
    },
    "ui": {
      "host": "192.168.0.240",
      "enabled": true
    },
    "pubsub": {
      "host": "192.168.0.240",
      "port": 8085
    }
  }
}

Now

"host": "192.168.0.240",

this could be any IP address or host like localhost or 10.20.0.25 or private IP like mentioned above.