3

With WebSockets, when clients should be notified that data changed, I've seen two approaches:

  1. the server pushes the data modifications directly within the push;
  2. or the server pushes no data, and the client, on receipt, triggers an API call to get the updated data.

Which one would you use, and in which case?

In my case, the server does quite a lot of computation before serving data through HTTP endpoints (i.e. it's not just "serialize this database table"), so I think I'll opt for the first approach, so that these computations don't need to be done client-side as well as server-side.

But I would be happy to have your opinions on this before proceeding.

asked Sep 19, 2017 at 13:51
2
  • 2
    You would use the latter for "chatty" communication. Notify the client that something happened and maybe the type of thing it is. Then let the client decide when to call back to the API. Commented Sep 19, 2017 at 14:27
  • As always, "it depends". Christophe's answer is a very good one. :) Commented Sep 19, 2017 at 19:48

1 Answer 1

4

The following questions may influence your choice:

  • how long the server needs to compute its result ?
  • is the result for a unique request or is it broadcasted to several requesters ?
  • how large is the data (payload) to be pushed ?
  • are the clients always interested in all the results pushed ?
  • can some results be lost, or must data always be delivered ?
  • is it possible that clients loose connections or have connection quality issues ?

Based on these matters you may opt for a data push if:

  • the connections are reliable
  • or if the data is not too large
  • or if the client is always interested in the results
  • or if results need to be frequently updated
  • or if the results are needed as realtime as possible

You may be interested in a push notification or in Server-Sent-Events (SSE) in all the other cases, and for example to cope with:

Note however that SSE is not as well supported by browsers as websockets, certainly making the later the easier choice.

answered Sep 19, 2017 at 17:23
4
  • 1
    Excuse my ignorance, but what is SSE? Commented Sep 19, 2017 at 18:31
  • 1
    I think it's Server Sent Events Commented Sep 19, 2017 at 18:41
  • @sp00m Server Sent Events (W3C - Here a short tuto and another article Commented Sep 19, 2017 at 18:41
  • 5
    Just one consideration. If you go #2 (API calls after the notification), be sure that your server-side can handle a wave of requests as large as the number of targets of the message. I have seen waves of +30K concurrent requests, doing acknowledge of receipt, knocking down the server. Commented Sep 19, 2017 at 18:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.