2017-02-19 21:50 阅读 143


Please help me clear up some confusion.

Laravel allows communication with socket.io by having you set up redis:


To my understanding Redis simply holds the data in memory something similar to memcached? This allows third party software like socket.io to pick up the data. Is this really websocket behaviour though?

I know that you can also do something like this in PHP:

$address = 'localhost';
$port = 5600;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, $address, $port);

Why wouldn't they choose to something above instead of having you set up Redis? There is probably a good answer to this but I don't have that much experience with either Redis or websockets.

Any information on this would be appreciated.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    duanbei3704 duanbei3704 2017-02-20 01:27

    You need to think about persistance of connection. A Request in Laravel only lives for the time it takes to get a response out. Once response is sent back, application shuts down until a new request hits the index.php and Laravel boots again.

    So in fact, you can not establish a persistant connection this way. Socket.io for example, will let you connect to the service and remain connected. This is the main difference between a Rest and Websocket approach. In a Rest interface, the client continually polls the server... So if you have 1000 clients you have 1000 pesky little clients asking you if you have anything new every 30 seconds... annoying at best. But each time they ask, Laravel goes through the whole boot/shutdown process... nothing is persistant.

    Now when using Socket.io through a Node service, each client will connect and have a persistant connection to the Node instance (which is a single persistant thread... better suited for this)... Having this connection to the 1000 clients, the clients simply listen to the socket for something new...

    When a Laravel request creates an event that is of interest to the 1000 clients, it simply pushes the information to Redis queue... The Node instance reads from the Redis queue and can broadcast to the 1000 connected client as it has maintained the connection...

    It is a nice use of both PHP and Node technology as it highlights the strengths of both...

    Hope this helps...

    点赞 评论 复制链接分享