Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running puppeteer code in firebase cloud functions

I'm working on a personal project which is an app where a user can input their address & credit card info and click a button to buy a parking permit for one of the universities that I visit often.

I want to host my front end in firebase's cloud storage and I want to create a cloud function where puppeteer code can run with the information that I save to the firebase's realtime database.

Is it possible for a firebase cloud function to run the puppeteer code that buys a parking permit?

Since puppeteer doesn't work with cloud functions, can I use Heroku to host the puppeteer code?

like image 318
BKoo Avatar asked Dec 03 '22 20:12

BKoo


1 Answers

It's now possible to run Puppeteer inside Cloud Functions (as of August 13, 2018).

Note: some of these commands are in the "beta" SDK so will no doubt change in the future. The quick start guide contains the latest documentation.

1) At the time of writing, you need to use Node 8 and the beta components:

gcloud components update
gcloud components install beta

2) There's a "headless Chrome" example in the Node examples which shows how to create screenshot as a PNG (there are other options available though).

git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
cd nodejs-docs-samples/functions/headless-chrome

3) Deploy the component

gcloud beta functions deploy screenshot --runtime nodejs8 --trigger-http

4) Finally, you'll need to increase the memory allocation. By default, Cloud Functions only get 256Mb of memory so you'll get an error if you try to run the Puppeteer without first changing the memory settings. Open your project in the Cloud Console, select Cloud Functions, select your function and click edit. 512Mb wasn't enough for me so I went up to 2Gb.

Cloud Functions memory settings

like image 111
Ian Avatar answered Jan 05 '23 12:01

Ian