douying6206 2018-10-31 04:54
浏览 103

了解之间的连接:PHP + Redis + Node.js +套接字

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).

https://shorturl.at/ntyLV

Diagram 1:

enter image description here

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:

enter image description here

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:

https://socket.io/docs/

What is the key feature of Redis that makes everybody recommend it for this kind of projects.

Diagram 3:

enter image description here

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?

  • 写回答

1条回答 默认 最新

  • douxingsuo8809 2018-10-31 05:28
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀