Can you run /host firebase emulator that is accessible from public ip?

Viewed 2774

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.

2 Answers

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

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.

Related