Short Polling vs Long Polling vs WebSockets - System Design

Поделиться
HTML-код
  • Опубликовано: 25 окт 2024

Комментарии • 82

  • @VasQuezadilla
    @VasQuezadilla 2 года назад +47

    An advantage for short polling is that the server can remain stateless.
    Both long polling and web sockets needs to keep a directory of which server contains the connection to the client, which is used to send data to the client whenever it's ready

  • @jesusdueas7145
    @jesusdueas7145 2 года назад +2

    Finally someone with an accent I can understand!

  • @neutronstar6739
    @neutronstar6739 3 года назад +18

    short and concise, I love this. It makes it clear on suitable implementation based on use cases.

  • @gttmone
    @gttmone Год назад

    I liked the video as soon as you gave the example with Node.js backend. Props to you ;)

  • @glennadams7047
    @glennadams7047 4 года назад +3

    Great summary. My analogy for a web socket is a Telnet session.

    • @BeABetterDev
      @BeABetterDev  4 года назад +1

      Thanks Glenn! I was going to bring up telnet as an example but thought it would show my age ;)

    • @glennadams7047
      @glennadams7047 4 года назад +3

      @@BeABetterDev ha ha! I feel your pain !!!

  • @harrazmasri2805
    @harrazmasri2805 Год назад

    great gracious explanation, kudos

  • @wishmaker1443
    @wishmaker1443 3 года назад +2

    Short and clear! Thanks man!

  • @LinkinPark.Chester
    @LinkinPark.Chester 3 года назад +2

    A perfect video for understanding concepts.

  • @myrickchow3039
    @myrickchow3039 2 года назад +1

    Thank you. Your presentation is clear and helpful 😄

  • @workingTchr
    @workingTchr 3 года назад +14

    I've used short polling with much success. The load on the server is a lot less than with long polling or WebSockets (or server sent events, which you didn't mention). You can have the client check for the mere existence of a particular file on the server, and take no action if it's not found. If you're using shared web hosting and have more than a few clients, short polling is your only practical option. Also, if your users are in a protected network (school, corporation) that wants to inspect packets coming through, you won't be able to establish a websockets (or server sent events) connection. So, short polling isn't a bad as it seems. I know, it sounds horrible sending out Ajax requests every 0.5 seconds, but in the real world, it works pretty good.

    • @BeABetterDev
      @BeABetterDev  3 года назад +1

      Hey Dennis, I totally agree with you that often short polling is the most practical solution (especially for web applications). I do think there is a performance benefit to be had though if one is willing to go through the extra effort.
      I think at scale is where short polling starts to fall apart. You may find that you're spending a lot of $$$ on resources that are serving empty poll requests.

    • @workingTchr
      @workingTchr 3 года назад +1

      @@BeABetterDev Where would the extra $$ come in? If someone simply checks for a file and doesn't download anything, how would that incur an extra charge from the web host?

    • @chaxiraxi_ytb
      @chaxiraxi_ytb Год назад +2

      @@workingTchr It depends on your hosting provider. Some providers will rent you a server or a hosting service where you pay a monthly/yearly bill, and that's it. You don't have to bother about bandwidth, storage etcetera. However, other providers will bill you on your usage, and that's the perfect case where saving bandwidth and storage will benefits your wallet. That's when you will start to spend extra money for serving nothing with short polling.

  • @tomstravelingadventures
    @tomstravelingadventures 4 года назад +2

    Clear and precise explanation... Thank you!

  • @sakirbey7911
    @sakirbey7911 2 года назад +2

    great explanation, your videos are very helpful. Thank you Sir.

  • @amrmoneer5881
    @amrmoneer5881 3 года назад +3

    great explanation keep up the good work!!

  • @ninjaninja9954
    @ninjaninja9954 2 года назад +3

    short polling can have benefits. ie if you want to check if a client is online and the client can ping the rest api every 5 mins to check if it is online. While it pings and gets a response, you can put other useful data in that response.

  • @mdsadiqurrahman9786
    @mdsadiqurrahman9786 4 года назад +6

    Right on the point! Thank you for sharing :)

  • @aj_shela
    @aj_shela 2 года назад

    Good knowledge under 5 minutes👍

  • @kevingardner8609
    @kevingardner8609 3 года назад +1

    Great job; thanks for the video.

    • @BeABetterDev
      @BeABetterDev  3 года назад

      You're very welcome Kevin! Thanks for the kind words.

  • @edwardwong654
    @edwardwong654 9 месяцев назад +1

    Webhooks is another possible model that may be good, depending on your requirements.

  • @jaysonp9426
    @jaysonp9426 11 месяцев назад

    This was great, thank you. Came here to figure out why OpenAI is using short polling with the assistants api lol

  • @jannickbreunis
    @jannickbreunis Год назад

    Is a webhook called long polling? Great explanation.

  • @chihabahmed5207
    @chihabahmed5207 3 года назад +3

    What's a perfect use case for long polling?
    Any use case?
    Thanks

    • @Conlexio
      @Conlexio 3 года назад

      a queue system where you expect to not receive messages every second
      if the queue is empty you can wait a while and immediately act when data comes in, if there is data it will immediately grab it instead
      aws SQS allows long polling, but only up to intervals of 20 seconds for example

    • @gl3nda96
      @gl3nda96 3 года назад

      Let's say a chat application. There is a server and a client. Now, the client wants to know if there are any new messages. Here, we may use long polling. The server will send the client any new message when there are new messages.

  • @yoastertoaster8306
    @yoastertoaster8306 8 месяцев назад

    Great video thank you

  • @hanjang513
    @hanjang513 Год назад +1

    In case of long polling, what happens in client side while they are waiting for the message or timeout? Do they still work on different task(Async)? Or do they wait until they get the response from the server(Sync)?

    • @jacob_90s
      @jacob_90s Год назад

      That's completely dependent upon the language/library you're using to initiate the request. I would definitely try to take the async approach though.

  • @adamhughes9938
    @adamhughes9938 4 года назад +2

    I think a nice complimentary video would involve web hooks or some sort of asynchronous callback. Thanks a lot. Also why is short polling bad? At most it makes one connection to the resource just like the other methods. Is it the overhead of setting up connection?

    • @SpotCallerzWrestling
      @SpotCallerzWrestling 4 года назад +4

      I think it has to do with the numerous Rest calls that would be made. I currently have a game setup using short polling and I essentially have it waiting 5 seconds and checking Dynamo DB for updates. I’m 100% this needs to be swapped for a Web socket.

    • @ozzyfromspace
      @ozzyfromspace 3 года назад +1

      @@SpotCallerzWrestling I hope you were able to get set up with web sockets :) I recommend socket.io, which is like three lines of code! Short polling sucks because every time you make a (presumably TCP-style) request, you have to set up all the request headers, which can get very expensive if you're doing it over and over again. To be fair, most browsers today will cache the request response so you don't have to do it over and over again, but web sockets are awesome because after one upgrade handshake, you have a binary data exchange on a single, fully-duplex, TCP connection, which is perfect for a game! Anyways, best wishes ☺️

    • @furkan2640
      @furkan2640 3 года назад +1

      He mentioned you are wasting (CPU)cycles in the video with short polling. What he means by that there needs to be a thread or process responsible of this requests. Creating the request, sending the request, waiting response and freeing up the resource and you will have a delayed update depending on the interval you have set. In other case you would have a thread that's just waiting for response from the connection and you would be updated almost instantly.

  • @armkiller2011
    @armkiller2011 3 года назад +1

    Thank you

  • @TheAmazonGamers
    @TheAmazonGamers Год назад

    Thank you.

  • @arifmarendra6370
    @arifmarendra6370 Год назад

    Kafka short pooling right?

  • @KidCrippler
    @KidCrippler 3 года назад +2

    Wouldn't short polling be the best choice for every normal CRUD application?

    • @millenialmusings8451
      @millenialmusings8451 2 года назад +1

      Short polling should be good for things like weather updates and stock market ticker data or score boards

    • @awekeningbro1207
      @awekeningbro1207 Год назад

      if you want to create huge bills at the end of every month, sure.

  • @ahmedelmetwally1440
    @ahmedelmetwally1440 Год назад

    Thanks

  • @cbest3678
    @cbest3678 4 года назад +1

    Thanks for sharing .. Does the timeout has to be handled by client app or server ?

    • @BeABetterDev
      @BeABetterDev  4 года назад +3

      Hi there. It would have to be configured by both the client and server. The client needs to ensure its connection timeout is greater than the server. The server would need to be implemented such that for any request, it will keep the connection open until a message becomes available or the timeout is hit.
      Hope this helps.

  • @hemanthvijay8206
    @hemanthvijay8206 Год назад

    I made one project controlling switches on frontend(react js) and backend server(node js) . multiple users can control switch and all other users should also get get updated status of switch .what should i use in this case polling or web socket or server side event . i used api to post and save data and other client get update the status every 5 seconds from server it works for some time and then i get error net::ERR_INSUFFICIENT_RESOURCES .hope you understand and give me a solution

  • @shahamitsynygy
    @shahamitsynygy 3 года назад +3

    Nice video. A couple of questions
    1. Did you miss SSE (server sent events) which is another variant for server push mechanisms?
    2. Is the connection type between client and resource the same for long polling and websockets? I guess websockets communication happens using a different protocol over HTTP.
    Thanks

    • @snghnishant
      @snghnishant 3 года назад

      SSE is not server push it’s just push. Server push comes with http2 which no one is able to use so far

    • @snghnishant
      @snghnishant 3 года назад +1

      Websockets establish a TCP connection at start same as long polling and both uses http for further data transmissions

    • @Giorgi-ho8rj
      @Giorgi-ho8rj 3 года назад

      @@snghnishant hey man where can I watch more detailed video about this?

    • @snghnishant
      @snghnishant 3 года назад +1

      @@Giorgi-ho8rj check Hussein Nassar channel

  • @Mehraj_IITKGP
    @Mehraj_IITKGP 11 месяцев назад

    Summary:
    In long polling, the server does not respond to client's request till the data is available and in short polling, the server responds the client regardless of the data availability.
    WebSockets provide a full two-way communication channel over a single, long-lived connection between a client and a server. Unlike traditional HTTP communication, which follows a request-response model, WebSockets allow both the server and the client to send messages independently at any time.

  • @skyeshore5704
    @skyeshore5704 3 года назад +1

    Web sockets: but how can the server(resource) positively know if the client is still connected?

    • @CrushedAsian255
      @CrushedAsian255 3 года назад

      Ping requests

    • @awekeningbro1207
      @awekeningbro1207 Год назад

      when a client disconnects, it sends a signal to the server from which server knows that the client has disconnected

  • @martinzen
    @martinzen 4 года назад +1

    How does this tie to Lambda/serverless workflows?

    • @Andrew24wow
      @Andrew24wow 4 года назад +3

      the only viable way to use a serverless function is it to be exposed to a API gateway w/ websockets. Lambdas are not dependable for waited request, as they will sometimes just die after x amount of time. If low-latency and reliability is key, then just host the websocket/api server yourself. Another issue with lambda is cold starts as well.

    • @VishalRaoOnYouTube
      @VishalRaoOnYouTube 4 года назад

      AWS API Gateway now supports the creation of WebSocket APIs: aws.amazon.com/blogs/compute/announcing-websocket-apis-in-amazon-api-gateway/

  • @preritjain3980
    @preritjain3980 4 года назад +2

    I was looking for the time/resource cost associated with setting up the connection. Only with that information, we can say how bad short polling could be.

  • @skt7088
    @skt7088 10 месяцев назад

    Any implementation in github?

  •  2 года назад +1

    Pretty sure short polling is not *necessarily* a bad choice if you have a system where the data is guaranteed to change - e.g stock prices.

    • @avadhoot05
      @avadhoot05 Год назад

      yes. but I think much better choice would be websocket. unlike short polling, the client doesn't need to open a connection for every request.

  • @arbazadam3407
    @arbazadam3407 3 года назад

    Is long polling an example of streams i.e. reactive programming?

    • @awekeningbro1207
      @awekeningbro1207 Год назад

      nope but polling can create streams of data for server to listen to or react to

  • @phanhuy5350
    @phanhuy5350 3 года назад +1

    nice.

  • @Euquila
    @Euquila Год назад

    Short polling is very useful when dealing with separate systems and one system is waiting for another to -- say -- boot up, or complete some flow.

  • @HenryPan
    @HenryPan 3 года назад +1

    Cool

  • @penizesouta
    @penizesouta 2 года назад +1

    From architecture point of view, all of them are wrong. Messaging or streaming is the correct way how to do this.

  • @jayak3768
    @jayak3768 7 месяцев назад +2

    You failed to explain the difference between long polling and web sockets.

  • @gena8414
    @gena8414 3 года назад

    rezorce

  • @zes3813
    @zes3813 3 года назад

    wrrr

  • @gafurov_1543
    @gafurov_1543 4 месяца назад

    long polling is TOO short