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 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示