0

I am developing a Web Application where a user will signup either through Email or Phone Number.

I have a long code to execute including Validation, Sanitization, Escaping, Session and some DB related tasks etc. After that it decides whether to send email or text message to user for confirmation. Sending an Email or Text Message sometimes takes a little bit time and until that user waits for response from server. Therefore, I thought of sending confirmation through a Thread so, that I can immediately return response to user. P.S. I am new to Threads in PHP.

if(//Validate)
{
     //Sanitize & Escape
     if(//Everything is good)
     {
         Thread.start()
         {
             //Needs to send Email or Text Message for confirmation
             //Should I start a Thread here which sends confirmation so that as soon as a thread is started 
             //a response is sent back to user?
         }

         //Return response, Confirmation sent (User will not have to wait until confirmation is sent 
         //because that is being sent through another thread)
     }
}

My Concerns are following

1) Should I be using Threads for sending confirmation message? Because otherwise user will have to wait for a response until a message is not completely sent which takes some time.

2) I read about pThreads at How can one use multi threading in PHP applications but it says that I should not be using pThreads in web server environment. Is he talking about the scenario I am doing? Should I use pThreads for my app?

3) Is pThread is same as Thread in php?

Airy
  • 5,484
  • 7
  • 53
  • 78
  • Usually, it's not the connection between PHP and the mail server that takes time. It's usually the mail server that puts the message in a queue before it is sent that takes time, which doesn't really depend on your PHP code. Have you done any debugging to see what part of your application it is that takes time? – M. Eriksson Mar 31 '20 at 06:47
  • @MagnusEriksson Thank you for response. Actually that's why I thought of using Threads so whatever the reason behind slowness is, it can put that sending work in a thread and immediately return a response to user. Currently I am on development machine. – Airy Mar 31 '20 at 06:49
  • _"that's why I thought of using Threads"_ - I think you're missing my point. Please read my comment again. – M. Eriksson Mar 31 '20 at 06:54
  • If you have processes which can be decoupled and run on demand, have you thought of using message queues instead. The page can send the request into the queue and then return to the user. Background processes can work through the queues in sequence and do any processing needed. I've used RabitMQ and it gives some useful features (such as ensuring that if a process fails, the job isn't lost). – Nigel Ren Mar 31 '20 at 07:22
  • @NigelRen Sir, I do not want to queue this confirmation process as I want to do it immediately but through a Thread. So, that when a thread is started, it will do the time consuming job like sending email or text along with returning response to user. The user will get response from server while the confirmation would still being sent – Airy Mar 31 '20 at 07:32
  • 1
    The message in the answer you point to should be a hint *Warning: The pthreads extension cannot be used in a web server environment*, it's not a should not - it's a cannot use threads in a web environment. – Nigel Ren Mar 31 '20 at 07:37

0 Answers0