Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to send a message during a "real" process #7

Open
etshy opened this issue Jan 24, 2019 · 9 comments
Open

How to send a message during a "real" process #7

etshy opened this issue Jan 24, 2019 · 9 comments
Labels

Comments

@etshy
Copy link

etshy commented Jan 24, 2019

I read your docs but I don't really understand how I can send a message during a process.

In my case, for exemple, I'd like to "dispatch/broadcast/push" (I don't know what's the correct term) a message from server to specific clients after an Article is created.

For example, I go on a page /article/create and I submit the form to create the Article.
As soon as the Article is created, I'd like to push a message for all the clients "subscribed" to this article's category. The response of this request must a "classic" Response, for the page to reload, etc., soI don't know what to do (and where) to send the message to the clients.

Where do I need to put the logic to create the StreamedResponse and send the message ?

ps : I'm using Symfony 4.2

@etshy etshy changed the title How to send a message after a persist operation How to send a message during a "real" process Jan 24, 2019
@hhxsv5
Copy link
Owner

hhxsv5 commented Jan 25, 2019

Hello, thanks for your question, depending on your scenario, maybe WebSocket is more suitable. You can use the asynchronous task queue to post tasks to the queue after creating the article and then broadcast it during the consumption process.

SSE is a protocol used to keep connections and continuously output data. Under FPM, an sse connection will occupy a process for a long time until Nginx times out.
If you have low performance requirements, you can use SSE.

  1. Add a flag field to the article table to indicate whether it has been broadcast.
  2. Create an SSE API, scan article table by the flag field in the Update callback.

@etshy
Copy link
Author

etshy commented Jan 25, 2019

Actually, with the Symfony Messenger Component, I have some asyn tasks to create a Notification object (that contained a link to the Article), so I'd like to send those Notificiation (in a new async Message, maybe?).
But if I make a async task, I don't understand how the StreamedResponse could be "sent" to a client.

Also your talked about Nginx, it can't work with apache ?

@hhxsv5
Copy link
Owner

hhxsv5 commented Jan 25, 2019

The premise of using async task is to build a WebSocket server, you can try Swoole, WorkerMan, ReactPhp.

If you use SSE, when the client establishes a connection, SSE will start an infinite loop to hold the connection, ensure that the connection will not be closed, the loop body always checks the data state, and if the state meets some condition, the data is output to the client. This is cross-process, a connection occupies a process, variable/memory can't be shared, only share central storage such as MySQL and Redis.

The workers of Nginx and Apache are generally multi-process model, they support SSE.

@etshy
Copy link
Author

etshy commented Jan 25, 2019

Oh OK.
I didn't think it would take a process ad make some infinite loop (I thought it was a bit "smarter" than that).

So one connection is related to one client (tab ?) ? I guess there is some way to identify the client to send him specific data then ? how do we have that information (like a user ID, or anything) ?

That's not my case (and surely it will never be on the app I work on) bu if there iare a lot of users connected at the same, doesn't it really heavy on the server ? to have as much process ongoing, I mean.

@hhxsv5
Copy link
Owner

hhxsv5 commented Jan 25, 2019

https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events

This document will help you understand how SSE works..

@etshy
Copy link
Author

etshy commented Jan 25, 2019

Thanks a lot.

For those who have the same question (how to send a message to a specific user) this simple answer help me : https://stackoverflow.com/a/35007680/4296405.

I don't know if it's what I searched but I'll give it a try.

@hhxsv5
Copy link
Owner

hhxsv5 commented Jan 30, 2019

It turns out that your question is how to send a message with the specified user from the client. My answer is from the server 😂.

@etshy
Copy link
Author

etshy commented Jan 30, 2019

Well as the client is initiating the connection, I think the easiest solution is to include the user id or some identifiers in the URL used, so every users have its own SSE url.

That and I didn't really understand how to send data to a specific client from server, without sending the user id in the url...

@hhxsv5
Copy link
Owner

hhxsv5 commented Jan 30, 2019

The URL contains the user ID, and the client establishes an SSE connection according to this URL. The server(FPM) occupies a process to keep the connection, and outputting data on the server is send a message to the connection of current user, but cannot send messages to the connections of other users. Because of the cross-process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants