12 Replies - 330 Views - Last Post: 25 May 2020 - 01:48 AM Rate Topic: -----

#1 TheStudent555   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 06-February 19

How to reduce server response time with firebase code

Posted 24 May 2020 - 02:47 AM

Currently have Firebase code that takes a long time to execute. The script is responsible for authenticating and verifying a user (Firebase code). With the Firebase code, the server response time is around 4 seconds. Without the Firebase code, the response time is around 400 milliseconds. The Firebase code has to make a trip to Googles servers in order to complete its tasks.

The mobile client currently has a timeout value of around 2500 ms. After the 2500 ms has elapsed, I get a timeout error, without a response to the client. However, the script eventually produces the correct response but is never returned to the client. I want the client to get the server response after the script has finished executing. How can I do this?

One possible solution I can think of is just changing the timeout policy to 4 seconds. However, I believe this is too long of a time to make a user wait.

Is it possible to isolate the Firebase code? So that I have two scripts, were script A executes and returns a response to the client, while triggering script B so that it could execute the Firebase code and communicate with the Google servers?

Is This A Good Question/Topic? 0
  • +

Replies To: How to reduce server response time with firebase code

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,993
  • Joined: 08-August 08

Re: How to reduce server response time with firebase code

Posted 24 May 2020 - 04:57 AM

If you're using a framework like Symfony you can put your Firebase calls in message processors where they can wait as long as needed.

That said, are you using PHP at all? What language(s) are you using? I think you might have posted in the wrong place.
Was This Post Helpful? 0
  • +
  • -

#3 TheStudent555   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 06-February 19

Re: How to reduce server response time with firebase code

Posted 24 May 2020 - 05:38 AM

@CTphpnwb, Thanks for answering.

Using Java on client (Mobile : Android)
PHP on back-end server side.

Currently not using Symfony.

This post has been edited by TheStudent555: 24 May 2020 - 05:39 AM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,993
  • Joined: 08-August 08

Re: How to reduce server response time with firebase code

Posted 24 May 2020 - 05:50 AM

Is it PHP that's doing the authenticating? If so, you'd want to look into message processors. PHP is not multithreaded, so message processors are used to split tasks off to separate threads. This way, you can send your Firebase request to a queue and respond to the user while the queue is off handling the request. Then you periodically check the queue and when the response returns, handle it appropriately.

https://symfony.com/...s/rabbitmq.html

If you can't switch to a framework, you could potentially use system calls to execute a PHP script that makes the Firebase request, and then handle the response similarly to a framework, but that might be as much work as using a framework, but without all the benefits.

This post has been edited by CTphpnwb: 24 May 2020 - 05:51 AM

Was This Post Helpful? 0
  • +
  • -

#5 TheStudent555   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 06-February 19

Re: How to reduce server response time with firebase code

Posted 24 May 2020 - 07:34 AM

Yes, PHP is handling the authentication. Is it possible to get similar effect with the exec()?

I know that the exec() can trigger another script to execute. I was planning on placing my Firebase code in the second script. That way the first script can return a response to the client while the second script runs and verifies the user (Sends an email).

I know that its possible for the first script to not wait for the second script. Though I am having trouble implementing this.

Executed from ScriptA.php
exec("php ScriptB.php {$var1} {$var2} {$var3} {$var4} {$var5} > /dev/null &");




The above code does not work. But the following does


exec("php ScriptB.php {$var1} {$var2} {$var3} {$var4} {$var5}");



The second code snippet however takes too long. Could be because I am not using the "> /dev/null &" but the first snippet is not working. What could I be doing wrong? Is this the wrong way of going about this?

Current Environment:
Windows 10
WAMP Server using PHP 7.2

This post has been edited by TheStudent555: 24 May 2020 - 07:39 AM

Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,993
  • Joined: 08-August 08

Re: How to reduce server response time with firebase code

Posted 24 May 2020 - 08:01 AM

In the first case you need to send the output to a file that php can later access. This gets tricky, since you have to keep track of multiple files (each user will have their own) and error check and garbage collect. That's the kind of thing a framework will take care of for you.
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2751
  • View blog
  • Posts: 8,056
  • Joined: 15-January 14

Re: How to reduce server response time with firebase code

Posted 24 May 2020 - 11:38 AM

Exec runs in the same process, PHP is going to wait for that to finish. If you want to manually fork a new child process this is how you do it:

https://www.php.net/....pcntl-fork.php
Was This Post Helpful? 0
  • +
  • -

#8 TheStudent555   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 06-February 19

Re: How to reduce server response time with firebase code

Posted 24 May 2020 - 12:02 PM

Are message processors generally how most websites/apps handle verification through email (Sending an email to confirm account registration)?
Was This Post Helpful? 0
  • +
  • -

#9 Ornstein   User is offline

  • D.I.C Head

Reputation: 105
  • View blog
  • Posts: 216
  • Joined: 13-May 15

Re: How to reduce server response time with firebase code

Posted 24 May 2020 - 12:39 PM

Job schedulers/queues are a fairly standard solution in situations where you don't necessarily need to make the user wait for a given task to complete. (If I've understood what you're trying to do here, I don't think you'd see much/any benefit from a queue, but if you do deem it necessary you should be able to find a stand-alone package that doesn't require a whole framework.)

4 seconds does sound like a long time, although I suppose it doesn't violate the 10 second rule. Is this the Firebase Auth API you're using - or do you have some kind of serverless function somewhere?

Are you able to choose a region for the API? Maybe something in a country/state closer to your own server.

Do you know whether the delay is because the endpoint is doing some particular work? i.e. Does it only make you wait 4 seconds when it has to send a verification email - or is it consistent?

(For simplicity's sake, I'm assuming you've ruled out other obvious performance- and connection-related things.)
Was This Post Helpful? 0
  • +
  • -

#10 TheStudent555   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 06-February 19

Re: How to reduce server response time with firebase code

Posted 24 May 2020 - 12:58 PM

Yes, I am using the Firebase Auth API. I am using the Auth API to create accounts and send a verification email. The Auth API is running on wamp server.

I get 4 - 6 second response time when I try to create the accounts and/or send a verification email. Even when I black out the create account code and only use the email verification code, the response time is at least 4 seconds. Without the Firebase code, the response time is at least 300 ms - 400 ms (Mobile app does not timeout).
Was This Post Helpful? 0
  • +
  • -

#11 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2751
  • View blog
  • Posts: 8,056
  • Joined: 15-January 14

Re: How to reduce server response time with firebase code

Posted 24 May 2020 - 01:06 PM

So basically, it's taking 4 seconds to send an email. That's a long time, that's not normal. While you can look for ways to allow your app to still work with more delay, which is a good idea for people with bad connections, it's worth looking into why it takes so long to send an email. How are you doing it, are you sending through a remote server? Can you change to a local server which would maybe then forward to the remote server?

The way a language like PHP works to send email, using a local mail server, is that the email doesn't actually get sent yet. It's even not correct to call it a mail server, it's a mail exchanger that PHP communicates with. When you use a function like mail, or a lot of the various things like phpmailer, PHP connects to the mail exchanger however it's configured to do so and sends it the message. The mail exchanger responds and PHP returns when the mail exchanger simply accepts the mail for delivery. It doesn't wait to actually send, the mail exchanger basically just tells PHP that the mail looks OK, and it's going to try to send it. So there's no reason that process should take 4 seconds unless there are serious network problems or other configuration problems. If you do need to send email through a remote server then look into setting up a local mail exchanger that will then send the emails to the outgoing server.

If you're using some kind of API that does actually wait until the email is actually sent, that might be a cause also.
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,993
  • Joined: 08-August 08

Re: How to reduce server response time with firebase code

Posted 24 May 2020 - 02:10 PM

View PostArtificialSoldier, on 24 May 2020 - 12:38 PM, said:

Exec runs in the same process, PHP is going to wait for that to finish.

True, but the ampersand should kick off a new process and then return, so the new process should have another thread. It's been a long time since I did that, but I remember it working.
Was This Post Helpful? 0
  • +
  • -

#13 Ornstein   User is offline

  • D.I.C Head

Reputation: 105
  • View blog
  • Posts: 216
  • Joined: 13-May 15

Re: How to reduce server response time with firebase code

Posted 25 May 2020 - 01:48 AM

If you make other API calls for simpler things (e.g. getting a user's details), does that also take a while to complete?

All requests to the API taking 4+ seconds seems fishy to me. Could you post your PHP code pertaining to the Auth API?

Also, are you reasonably sure your server's network isn't a problem here? e.g. Your server has no outbound connection limits/throttling, you've otherwise tested the connectivity/response from the server to the API endpoint using cURL/ping/HEAD/OPTIONS, you've tested the code/connectivity from multiple networks (e.g. local & VPS), etc? Just to rule that out.

You may/should also be able to interact with the API directly from the app, in most cases - which would reduce the dependency/load on your own server. In this case, you might for example have the app itself interact directly with the API to do the sign in and email verification - and then send the ID token to your server, which might use the getAccountInfo endpoint to confirm that the account exists and the email is verified, etc.

(If I'm not wrong, the API key in this instance is not sensitive; you may want to confirm that for your own peace of mind and also look into any security settings/policies Firebase offers to restrict how/where/when the API key can be used, etc.)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1