A
A
aanon2017-08-04 20:59:21
Redis
aanon, 2017-08-04 20:59:21

How to properly organize WebSocket and Laravel 5?

Hello. I really need an explanation of one problem, I'll try briefly.
I'll start with the task: you need to make search results on websockets. That is, the user entered a request, the browser connected to the server via a websocket, when the next result is found, the server sends it to the browser and the browser displays it. The web socket is needed because the search can take at least half an hour (a very large number of different sources), and I don’t want to use ajax here (so as not to constantly poll the server and not load it), but simply loading the page is completely excluded due to the formulation of the task itself ( long lead time). The websocket event model in general looks very attractive.
Now about the problem. I have read and viewed a lot of materials about the bundle, which is probably the most popular. Node.js is installed, socket.io is loaded into the browser. The browser communicates with node.js. node.js itself communicates with laravel via redis. Laravel broadcasts an event with the name of the channel, it is (apparently) written in redis, and node.js listens to redis, picks up a new event and sends it to all browsers listening to this channel.
But I need to:
a) Pass the search parameters to Laravel from the browser
b) Return the result of processing from Laravel to a specific browser, and not to everyone in a row in general
Point "b" is not disclosed anywhere at all. If the user is not authorized, then it is completely (to me) not clear how laravel will pass something to him, because laravel does not know who exactly requests the result.
I can't find answers to these questions anywhere, and I don't even know where I'm going. Has anyone come across it and can advise?
I will add that I don’t want to throw out Laravel from here. The search functionality is already there. Writing a demon in PHP and somehow putting this functionality there is a dubious matter ...
I will add that somehow calling Laravel from node.js is also not suitable - then you have to wait for Laravel to fully work. In theory, it is necessary to somehow make it so that at the user's request, Lara starts searching, and in the process of searching, right in a huge cycle, throws events outward about new data found.
It is possible that I do not understand how it works at all and whether it can work this way, because. encounter for the first time.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Semenko, 2017-08-04
@abler98

the search can last at least half an hour (a very large number of different sources)

Maybe then you should send a search request, put it in a queue, and write the result to the database, then the user will not have to wait.
You can also connect communication with the server via sockets here to immediately display the search result if the user has not gone anywhere yet, but it is still better to write the results of such a lengthy operation to the database.
As I said, we send a normal request to the server, but we do the search itself in a queue . Upon completion of the search, we write the result to the database and broadcast the desired event.
There are private channels for this, more details in the documentation: https://laravel.com/docs/5.4/broadcasting#authoriz...
PS Laravel allows you to implement this without much effort, everything is ready, you just need to carefully read the documentation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question