0

I am working on a project that has pure PHP as backend and MySQL as database. Currently I am using ajax to post and receive data from the database. But I would like to use Websockets for the receiving part to make it realtime. From what I have searched and found people are referring to Node.js, Redis PubSub, etc to achieve it. However I have failed to find a good tutorial on the subject. Also to my knowledge, Nods.js and PHP doesn't get along together that well when I have complex PHP algorithms and MySQL queries running at the back. So my question is very simple. Is there anyway to use Websockets with PHP and MySQL without using anything else, especially Node.js.

Saptarsi
  • 81
  • 2
  • 11

1 Answers1

-1

Downvoting due to your lack of experience (which is absolutely normal for everyone to be), and that you ask broad question not related to any particular problem. Too broad.

You have to have WebSockets connection and php script that gets request by AJAX should be able to somehow send message to socket related to client that is made request in first place (to simulate request/response). But there is no good benefit here to be honest.

You might consider writing fully WebSockets based app in PHP that would listen on messages from clients, and perform same logic as currently it does to deal with database. Then once it is ready - send response.

But such architecture is not meant initially to be executed by PHP, as you need non-blocking logic, and each PHP script is executed in blocking manner, so or you somehow find a way to deal with it. Or you might consider different solution and possibly language.

There is no problem to have PHP and node.js together, but you should understand that they are different processes and have to respect that from technical point of view.

moka
  • 22,846
  • 4
  • 51
  • 67