{"id":1781,"date":"2021-03-10T12:00:00","date_gmt":"2021-03-10T09:00:00","guid":{"rendered":"https:\/\/amorev.ru\/?p=1781"},"modified":"2021-08-05T11:35:21","modified_gmt":"2021-08-05T08:35:21","slug":"polling-and-long-polling","status":"publish","type":"post","link":"https:\/\/amorev.ru\/en\/polling-and-long-polling\/","title":{"rendered":"Polling and Long Polling"},"content":{"rendered":"\n<p>We live in an era of real time applications, where we use convenient and fast socket connection for the actual data transfer, which allows to send an event straight to the client's device.<\/p>\n\n\n\n<p>But it's not always easy to set the socket. Sometimes the host doesn't allow it, and sometimes there just no resources to support the infrastructure, test fault tolerance and security. In this case polling can be used.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>Polling<\/strong>. The very name of it tells us that some poll will be happening regularly. Here it will be the poll of server.<\/p><\/blockquote>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/amorev.ru\/wp-content\/uploads\/2021\/03\/photo-1571609549239-bf07fb79f702-1024x768.jpg\" alt=\"\" class=\"wp-image-1788\" width=\"575\" height=\"431\" srcset=\"https:\/\/amorev.ru\/wp-content\/uploads\/2021\/03\/photo-1571609549239-bf07fb79f702-1024x768.jpg 1024w, https:\/\/amorev.ru\/wp-content\/uploads\/2021\/03\/photo-1571609549239-bf07fb79f702-300x225.jpg 300w, https:\/\/amorev.ru\/wp-content\/uploads\/2021\/03\/photo-1571609549239-bf07fb79f702-768x576.jpg 768w, https:\/\/amorev.ru\/wp-content\/uploads\/2021\/03\/photo-1571609549239-bf07fb79f702.jpg 1267w\" sizes=\"auto, (max-width: 575px) 100vw, 575px\" \/><figcaption>\u0418\u0437\u0431\u0438\u0440\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0443\u0447\u0430\u0441\u0442\u043e\u043a, \u0433\u0434\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u0439 polling:)<\/figcaption><\/figure><\/div>\n\n\n\n<p>Of course we won't vote. But still we will question the server. The idea is quite plain - regularly ask the server about the new events and updates.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Ordinary polling<\/h2>\n\n\n\n<p>For example, we write a messenger and want to deliver new messages to users. With ordinary polling the client's application (browser\/phone) will throw a request <code>GET \/messages<\/code>every N seconds (e.g. 2 sec).<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"322\" height=\"505\" src=\"https:\/\/amorev.ru\/wp-content\/uploads\/2021\/03\/Polling.png\" alt=\"\" class=\"wp-image-1784\" srcset=\"https:\/\/amorev.ru\/wp-content\/uploads\/2021\/03\/Polling.png 322w, https:\/\/amorev.ru\/wp-content\/uploads\/2021\/03\/Polling-191x300.png 191w\" sizes=\"auto, (max-width: 322px) 100vw, 322px\" \/><figcaption>Polling communication between client and server<\/figcaption><\/figure><\/div>\n\n\n\n<p>Direct decision leads to a needed result - a user gets his messages not later than 2 seconds after they appeared. At the same time cons are pretty obvious:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>It's still necessary to wait 2 seconds to get some messages<\/li><li>The server load is unreasonably high. It's necessary to create a connection every 2 seconds, initialize an application etc. All this leads to extra waste of resources<\/li><\/ol>\n\n\n\n<p>And in this moment we welcome the <strong>long polling<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is long polling?<\/h2>\n\n\n\n<p>It's the same polling, but with slightly other idea. Within long polling the client sends a request to server with created beforehand timeout (usually 30 sec). The server processes this request in an infinite cycle mode with specified timeout and within the cycle awaits the updates needed by a client. When the update appears, the server at once gives the answer and closes the connection.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"336\" height=\"570\" src=\"https:\/\/amorev.ru\/wp-content\/uploads\/2021\/03\/Long-Polling-1.png\" alt=\"\" class=\"wp-image-1787\" srcset=\"https:\/\/amorev.ru\/wp-content\/uploads\/2021\/03\/Long-Polling-1.png 336w, https:\/\/amorev.ru\/wp-content\/uploads\/2021\/03\/Long-Polling-1-177x300.png 177w\" sizes=\"auto, (max-width: 336px) 100vw, 336px\" \/><figcaption>Long polling communication between client and server<\/figcaption><\/figure><\/div>\n\n\n\n<p>So we shake off two cons of polling and provide updates to the user with the minimum delay. Long polling also has its disadvantages, but there is web socket for resolving them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We live in an era of real time applications, where we use convenient and fast socket connection for the actual data transfer, which allows to &hellip; <\/p>\n","protected":false},"author":1,"featured_media":1788,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[265,49],"tags":[269,268,267,266],"class_list":["post-1781","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-likbez","category-razrabotka","tag-api","tag-http","tag-polling","tag-likbez"],"_links":{"self":[{"href":"https:\/\/amorev.ru\/en\/wp-json\/wp\/v2\/posts\/1781","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/amorev.ru\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/amorev.ru\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/amorev.ru\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/amorev.ru\/en\/wp-json\/wp\/v2\/comments?post=1781"}],"version-history":[{"count":0,"href":"https:\/\/amorev.ru\/en\/wp-json\/wp\/v2\/posts\/1781\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/amorev.ru\/en\/wp-json\/wp\/v2\/media\/1788"}],"wp:attachment":[{"href":"https:\/\/amorev.ru\/en\/wp-json\/wp\/v2\/media?parent=1781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/amorev.ru\/en\/wp-json\/wp\/v2\/categories?post=1781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/amorev.ru\/en\/wp-json\/wp\/v2\/tags?post=1781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}