Real-time Data Updates with Fetch API - JavaScript Tutorial

Поделиться
HTML-код
  • Опубликовано: 9 сен 2024
  • In this video I'll take you through the process of using short polling in combination with the Fetch API to achieve the effect of "real time" or "live" data updates with JavaScript for your web projects.
    Short polling involves asking the server for a fresh copy of information every set amount of seconds. The example in this video demonstrates a live view count for a piece of media content.
    Support me on Patreon:
    / dcode - with enough funding I plan to develop a website of some sort with a new developer experience!
    For your reference, check this out:
    developer.mozi...
    Follow me on Twitter @dcode!
    If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!

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

  • @kuhak4780
    @kuhak4780 2 года назад +17

    This is not an ideal solution for live data pulling/update. It will keep calling data from server. Imagine if 2 million requests done this way simultaneously, it will overflood the server with requests etc.

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

      Then would websockets be a better solution ?

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

      @@tylerobier3269 I think websocket is better for one-to-one chatting

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

      so, what is the best solution?

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

    you just saved our project. thanks.

  • @Fathurrr1000
    @Fathurrr1000 2 года назад +5

    is that gonna slow down the performance when we refetch >100 table data every 2 seconds?

  • @aarkaev
    @aarkaev 5 лет назад +42

    It's NOT "real-time". It's periodic updates

    • @dcode-software
      @dcode-software  5 лет назад +7

      I see your point, something like Websockets would achieve pure real time. If you poll fast enough then users may not notice the difference

    • @aarkaev
      @aarkaev 5 лет назад +8

      @@dcode-software Server Sent Events may be better choice without WebSockets. Anyway thanks for video

    • @rishiktiwari
      @rishiktiwari 4 года назад +5

      @@dcode-software Yes, but the problem with polling is the amount of burden on server. Imagine thousands of clients polling every second.

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

      @@rishiktiwari Any suggestion to avoid burden on server? or server side script?

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

      @@vikydxn You can use websockets (if PHP then try Ratchet or Signalr if .NET). Alternatively you can use third party API like Pusher.
      Also, if your requirement is not fully real-time then fetching every 3-5mins shouldn't be much of a problem.

  • @theartist8835
    @theartist8835 3 года назад +6

    Would using server-sent event be an alternative to this technique ? which one is more performant ?

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

    Can we update data with python insteas of php

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

    Can I put url of a website in data, will it fetch the data as the whole website is only having a four digit number which change after every refresh.
    Please reply it is very urgent

  • @tusharniras
    @tusharniras 3 года назад +5

    Well its not real-time but it solves my purpose. Really helpful video! Thanks!

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

    thanks for the video.. I don't know it could be as simple as this to make the API and also fetching the data (I'm new to AJAX etc) and also so fast when you clicking that TAB button 😂😂

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

    How can I modify this code to send Ajax request to PHP to update a database value every 30days

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

      For the 30days countdown you will have to make sure that the client keeps your application open till that period because the countdown is on client's computer. I would prefer using Cookies or Redis keys with 30 days expiry. If you have access to the hosting service then you can set a cron job which runs your scripts every 30 days.

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

    What about 100ms? What's the safe minimum interval?

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

    Awesome as always 👍🎉👌

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

    Good tutorial thanks. I'm wondering if I need to use data as a parameter for another function out of setInterval, I mean get the current data and use that data for a different function for example create a function with an alert(), and run it only once and not every two seconds. What do you recommend for this task?

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

      let x = 1;
      async function function1() {
      await ..(some operation);
      x = 4;
      return x;
      }
      async function function2() {
      await function1();
      x = 5;
      return x;
      }
      console.log(x);
      result: console shows "5"
      I don't know if this is the best way, but it works.

  • @3dgiftz
    @3dgiftz 2 года назад

    Thanks

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

    is their a way to use javascript instead of PHP to append view count

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

      PHP generates the JSON data structure, so you could do it any language of your choice (node/python/ruby/whatever), as long it is JSON and fetchable.

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

      Use Node.js for backend!

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

    Boa aula, porém acredito que um setinterval a depender do tamanho da aplicação e do número de requisições haja necessidade de um servidor de grande processamento

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

    Thanks bro

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

    thank you

  • @johndeoresearch2251
    @johndeoresearch2251 5 лет назад +1

    can you share the code?

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

    Thank youu

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

    that's cool...

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

    GOOOOODDDSS TO!

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

    can i do this in table data?

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

    multiple rows data updated is possible? if yes please help me.

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

    what is youtube.local?

    • @dcode-software
      @dcode-software  4 года назад +1

      I explain it in detail in this video:
      ruclips.net/video/gBfZdJFxjew/видео.html

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

      @@dcode-software Thanks

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

    Java has a footer in the middle of the official site 💀

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

    ll

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

    PHP 🤮