I just want to clarify (with your help) the connection between: PHP + Redis + Node.js + Sockets
.
For that I took as a base example the project on:
https://github.com/biswassampad/Realtime-Chat-With-Laravel5.6-and-Socket.io
This exmaple is a very basic chat application.
Here you have a demo video: https://www.youtube.com/watch?v=AMgYpjFXnWo
I downloaded it to local and worked fine.
I noticed that it makes use of: PHP
+ Redis
+ Node.js
+ Sockets
.
I would like to analyze different diagrams I created and pasted below which you can find on the following url (if you want you can edit them and export the worksheet link through: https://shorturl.at
and then paste the new images and / or link on this thread).
Diagram 1:
Question 1: Could you please, let me know if the diagram above is right or if I'm confusing / missing something relevant? That diagram is based on the GitHub
project referenced at the beginning of this post.
Diagram 2:
Question 2: What about if we get rid of Redis
?. Node.js
can create sockets, broadcast messages, etc., as you can see on the following documentation:
What is the key feature
of Redis
that makes everybody recommend it for this kind of projects.
Diagram 3:
Question 3: Why is not possible to do everything with PHP/Apache
on the server side?. Is not possible for PHP
to open sockets in the same way as Node.js
does?. What magic powers has Node.js
that PHP
doesn't?
[EDIT 1]
Question 4:
On the following tutorial:
https://medium.com/@adnanxteam/how-to-use-laravel-with-socket-io-e7c7565cc19d
which is also about PHP + Redis + Node.js + Sockets
, they use: laravel-echo-server
.
Why on the GitHub
sample project above is not used that plugin?
Question 1
I believe your diagram is right, but I do not know if some minor details are wrong. Though for me, that's the whole idea.
Question 2
Redis is the database being used to store the messages.
I think it is preferably used because it is fast (because the data is stored/queried in in-memory(RAM)). Though I think you can use other databases as well (MySQL, etc).
If you remove Redis, where will you store data (E.g., message to be broadcasted)?
Question 3
In the current context, Node.js is equivalent to Apache; not to PHP.
Now, Apache runs your PHP code (Laravel project) while Node.js runs your JavaScript code (Socket.IO).
I think you can implement a PHP variant of Socket.IO as well; and it will be ran in a separate instance. It's just maybe not popular or like every other problems, there are better tools which provide better solutions.
As googled, PHP would be a bad solution for broadcasting messages since it is blocking, meaning, it will finish the current request first before going to the next one. While on the other hand, Node.js is non-blocking.
From Wikipedia: https://en.wikipedia.org/wiki/Node.js
The biggest difference between Node.js and PHP is that most functions in PHP block until completion (commands execute only after previous commands finish), while Node.js functions are non-blocking (commands execute concurrently or even in parallel, and use callbacks to signal completion or failure).
Disclaimer: I'm not a total expert in this field. My answer is based from experience with some touch of googling.