alexandrevicenzi / go-sse

Server-Sent Events for Go
MIT License
172 stars 40 forks source link

Send SSE data only when it received POST method #20

Closed ljfreelancer88 closed 4 years ago

ljfreelancer88 commented 4 years ago

Hi, newbie here. At the given example, it pushes tha data every 5sec. How can can I make the SSE push only when there's a POST(Hey subscribers, new data is coming!) method that kicks. Because I don't think it is wise to keep emitting even there's no new data.

Thank you in advance.

alexandrevicenzi commented 4 years ago

SSE opens a long term connection, that's how the protocol works, you may want to learn more about it here.

You send data to the client when you have new data, so you should call SendMessage only when you have new data. This is validation on your side, not the library.

By protocol standard, once a client connects it's ready to receive data, there's no need to receive some POST from clients to start receiving data, and also, the protocol and the library supports replay of old messages in case of disconnection (using the Last Event ID).