﻿
{"id":10046,"date":"2018-12-30T19:22:50","date_gmt":"2018-12-30T19:22:50","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=10046"},"modified":"2018-12-30T19:22:50","modified_gmt":"2018-12-30T19:22:50","slug":"node-js-websocket-nedir-nasil-kullanilir","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/node-js-websocket-nedir-nasil-kullanilir\/","title":{"rendered":"Node.js &#8211; Websocket Nedir? Nas\u0131l Kullan\u0131l\u0131r?"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Bundan y\u0131llar y\u0131llar \u00f6ncesine kadar<span style=\"font-size: 9px;\">(yani ge\u00e7en y\u0131la kadar)<\/span> web yap\u0131lanmalar\u0131nda ger\u00e7ek zamanl\u0131 i\u015fler yap\u0131laca\u011f\u0131 zaman Ajax teknolojisi kullan\u0131larak, belli aral\u0131klarla <span style=\"font-size: 9px;\">1 saniye &#8211; milisaniye<\/span> yap\u0131lan sorgular neticesinde sanki ger\u00e7ek zamanl\u0131 bir sistem izlenimi veren yap\u0131lar \u00fczerinde \u00e7al\u0131\u015f\u0131yorduk. Dolay\u0131s\u0131yla sistemin canl\u0131l\u0131k durumunu kusursuz sa\u011flayabilmek i\u00e7in Backend&#8217;e yap\u0131lacak sorgu aral\u0131\u011f\u0131n\u0131 m\u00fcmk\u00fcn mertebe azaltarak, sistemin kullan\u0131c\u0131ya d\u00f6n\u00fck i\u015fleyi\u015finde ger\u00e7ek zamanl\u0131l\u0131k \u00f6zelli\u011fini artt\u0131rmay\u0131 tercih ederdik. Hal b\u00f6yleyken, mikro aral\u0131klarla yap\u0131lan her bir talep neticesinde bilan\u00e7o bizlere y\u00fcksek maliyet ve performans kayb\u0131 olarak yans\u0131maktayd\u0131. Vee tabi t\u00fcm bu s\u00fcre\u00e7 Http protokol\u00fc \u00fczerinden ger\u00e7ekle\u015fmekte ve yap\u0131lan her bir talebe kar\u015f\u0131l\u0131k ihtiya\u00e7 olmaks\u0131z\u0131n bir cevap al\u0131nmaktayd\u0131.<\/p>\n<p>Websoket teknolojisi ile TCP ba\u011flant\u0131s\u0131 \u00fczerinden \u00e7ift y\u00f6nl\u00fc mesaj g\u00f6nderebilme \u00f6zelli\u011fi sayesinde, backende micro aral\u0131klarla yap\u0131lan talep yo\u011funlu\u011fu neticesinde \u00f6denen y\u00fcksek maliyetten kurtulunmakta ve ger\u00e7ek zamanl\u0131 uygulamalar\u0131 d\u00f6rt d\u00f6rtl\u00fck bir \u015fekilde uygulayabilme \u015fans\u0131 elde etmekteyiz.<\/p>\n<p>Evet&#8230; Websoket teknolojisi; s\u00fcrekli a\u00e7\u0131k olan bir TCP ba\u011flant\u0131s\u0131 \u00fczerinden \u00e7ift y\u00f6nl\u00fc mesaj g\u00f6nderebilme i\u015flemini yapabilen bir protokold\u00fcr. Bu a\u00e7\u0131k TCP protokol\u00fc \u00fczerinden taraflardan isteyen istedi\u011fine mesaj g\u00f6nderebilmektedir ve HTTP protokol\u00fcnde oldu\u011fu gibi g\u00f6nderilen mesajlara cevap verme zorunlulu\u011fu bulunmamaktad\u0131r. O y\u00fczden burada &#8220;talep(request)&#8221; \/ &#8220;cevap(response)&#8221; terminolojisinden ziyade sadece &#8220;mesaj\/message\/data&#8221; terminolojisi tercih edilmektedir.<\/p>\n<div id=\"attachment_10060\" style=\"width: 1076px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/http-rest-request.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-10060\" class=\"wp-image-10060 size-full\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/http-rest-request.png\" alt=\"HTTP\/REST Request\" width=\"1066\" height=\"418\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/http-rest-request.png 1066w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/http-rest-request-300x118.png 300w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/http-rest-request-768x301.png 768w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/http-rest-request-1024x402.png 1024w\" sizes=\"auto, (max-width: 1066px) 100vw, 1066px\" \/><\/a><p id=\"caption-attachment-10060\" class=\"wp-caption-text\">HTTP\/REST Request<\/p><\/div>\n<p>Yukar\u0131daki \u015femadan HTTP\/REST Request i\u015flevselli\u011fini incelerseniz e\u011fer her bir requeste kar\u015f\u0131l\u0131k bir response oldu\u011funu g\u00f6receksiniz. HTTP protokol\u00fcnde requeste kar\u015f\u0131l\u0131k d\u00f6n\u00fclecek bir data olsun olmas\u0131n bir response d\u00f6n\u00fclmesi mecburidir.<\/p>\n<div id=\"attachment_10064\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/websocket.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-10064\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/websocket.png\" alt=\"Websocket\" width=\"792\" height=\"641\" class=\"size-full wp-image-10064\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/websocket.png 792w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/websocket-300x243.png 300w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/websocket-768x622.png 768w\" sizes=\"auto, (max-width: 792px) 100vw, 792px\" \/><\/a><p id=\"caption-attachment-10064\" class=\"wp-caption-text\">Websocket<\/p><\/div>\n<p>Websocket \u015femas\u0131n\u0131 incelerseniz e\u011fer a\u00e7\u0131k bir TCP ba\u011flant\u0131s\u0131 \u00fczerinden t\u00fcm s\u00fcre\u00e7 i\u015flemektedir. Handshake(el s\u0131k\u0131\u015fma\/tokala\u015fma) i\u015fleminin ard\u0131ndan Acknowledgement(onay\/tasdik) yap\u0131ld\u0131ktan sonra Bi-directional message(\u00e7ift y\u00f6nl\u00fc mesajla\u015fma) s\u00fcreci ba\u015flamaktad\u0131r. Taa ki connection end olana kadar.<\/p>\n<p>K\u0131saca Websocket teknolojisinde;<\/p>\n<ul>\n<li>Bir istekte bulunuldu\u011fu zaman cevap beklenilmemektedir. Tabi iste\u011fe ya da ihtiyaca g\u00f6re cevapta d\u00f6n\u00fclebilir.<\/li>\n<li>Client server&#8217;a, server client&#8217;a veri g\u00f6nderebilir. \u00c7ift y\u00f6nl\u00fc mesajla\u015fma.<\/li>\n<li>Server, gelen verileri abonelere iletir.<\/li>\n<li>\u0130ki taraf g\u00f6nderdi\u011fi mesajlara cevap beklememektedir.<\/li>\n<\/ul>\n<h3>Socket.IO Mod\u00fcl\u00fc<\/h3>\n<p>Node.js&#8217;de websocket i\u015flemleri i\u00e7in kullanaca\u011f\u0131m\u0131z mod\u00fcl socket.io mod\u00fcl\u00fcd\u00fcr. Socket.IO; websocket ile ilgili protokollerin tamam\u0131n\u0131 (XHR, Long Polling, Websocket) standartla\u015ft\u0131ran ve sabit bir kullan\u0131m sunan websocket em\u00fclat\u00f6r\u00fcd\u00fcr.<\/p>\n<p><strong>Hoca em\u00fclat\u00f6r ne la?<\/strong> diye soranlar\u0131n\u0131za genel k\u00fclt\u00fcr mahiyetinde a\u015fa\u011f\u0131daki a\u00e7\u0131klamay\u0131 sunal\u0131m.<\/p>\n<blockquote><p>Bir sistemin ba\u015fka bir sistem \u00fczerinde \u00e7al\u0131\u015ft\u0131r\u0131lmas\u0131na em\u00fclasyon, bu i\u015flevi yapan yaz\u0131l\u0131mlara em\u00fclat\u00f6r denir.<\/p><\/blockquote>\n<p>Socket.IO mod\u00fcl\u00fcn\u00fcn Node.js ile kusursuz bir kullan\u0131m\u0131 vard\u0131r.<\/p>\n<p>\u0130lgili mod\u00fcl\u00fc projenize entegre edebilmek i\u00e7in a\u015fa\u011f\u0131daki kodu \u00e7al\u0131\u015ft\u0131rman\u0131z yeterlidir.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nnpm i socket.io --save\r\n<\/pre>\n<h3>Server ile Client Aras\u0131nda TCP Ba\u011flant\u0131s\u0131 Olu\u015fturmak<\/h3>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst http = require(&quot;http&quot;);\r\nconst socket = require(&quot;socket.io&quot;);\r\n\r\nconst server = http.createServer((request, response) =&gt; {\r\n     response.end(&quot;...&quot;);\r\n});\r\nserver.listen(1234);\r\n\/\/1234 portuna gelecek olan t\u00fcm istekleri socket taraf\u0131ndan dinlememiz gerekmektedir.\r\nconst io = socket.listen(server);\r\n\/\/socket taraf\u0131ndan ilgili server dinlenmeye al\u0131nm\u0131\u015ft\u0131r. Dolay\u0131s\u0131yla gelecek olan t\u00fcm talepler socket taraf\u0131ndan yakalanacakt\u0131r.\r\n\/\/Bu \u015fekilde her talep neticesinde server ile client aras\u0131nda aktif bir TCP ba\u011flant\u0131s\u0131 olu\u015fturulacakt\u0131r.\r\n<\/pre>\n<p>\u00d6nceki yaz\u0131lar\u0131m\u0131zda kaleme ald\u0131\u011f\u0131m\u0131z <a href=\"http:\/\/www.gencayyildiz.com\/blog\/node-jsde-http-sunucusu-olusturalim\/\" rel=\"noopener\" target=\"_blank\">Node.js\u2019de Http Sunucusu Olu\u015ftural\u0131m<\/a> ba\u015fl\u0131kl\u0131 makalemizde belirtti\u011fimiz \u00fczere bir server olu\u015fturmaktay\u0131z. Olu\u015fturulan bu server&#8217;a kar\u015f\u0131 olan t\u00fcm talepleri, ba\u011flant\u0131lar\u0131 vs. dinleyebilmek i\u00e7in &#8220;socket.io&#8221; mod\u00fcl\u00fc taraf\u0131ndan ilgili server dinlenmeye al\u0131nmaktad\u0131r. Bu dinleme neticesinde ilgili server ile client aras\u0131nda aktif bir TCP ba\u011flant\u0131s\u0131 olu\u015fturulmaktad\u0131r.<\/p>\n<p>Server&#8217;a bir client taraf\u0131ndan ba\u011flant\u0131 sa\u011fland\u0131\u011f\u0131 zaman &#8220;connection&#8221; isimli bir event tetiklenecektir. Dolay\u0131s\u0131yla bu event client&#8217;\u0131n t\u00fcm bilgilerini beraberinde getirecektir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nio.sockets.on(&quot;connection&quot;, socket =&gt; {\r\n     \/\/Server'a bir client ba\u011fland\u0131\u011f\u0131 zaman &quot;connection&quot; isimli olay tetiklenecektir. socket parametresi ise ba\u011flant\u0131 yapan client'\u0131n bilgisini i\u00e7ermektedir.\r\n     console.log(&quot;User connection&quot;, socket.client.id);\r\n});\r\n<\/pre>\n<p>Bu noktadan itibaren server k\u0131sm\u0131n\u0131n in\u015fas\u0131 tamamd\u0131r. \u015eimdi bir client tasarlayal\u0131m ve a\u00e7m\u0131\u015f oldu\u011fumuz bu TCP ba\u011flant\u0131s\u0131na connect edelim. Bizler bu i\u00e7eri\u011fimizde client&#8217;\u0131 lalettayin bir html sayfas\u0131 olarak tasarlayaca\u011f\u0131z.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n-- client.html --\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n     &lt;script src=&quot;http:\/\/localhost:1234\/socket.io\/socket.io.js&quot;&gt;&lt;\/script&gt;\r\n     &lt;script&gt;\r\n          const socket = io.connect(&quot;http:\/\/localhost:1234&quot;);\r\n     &lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n     &lt;p&gt;\r\n          Merhaba, WebSocket\r\n     &lt;\/p&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Client taraf\u0131nda dikkat edilmesi gereken husus &#8220;socket.io.js&#8221; isimli JavaScript k\u00fct\u00fcphanesinin kaynak olarak eklenmesidir. \u0130lgili dosyaya, \u00e7al\u0131\u015fma yapt\u0131\u011f\u0131n\u0131z domain uzant\u0131s\u0131 \u00fczerinden &#8220;http:\/\/***\/socket.io\/socket.io.js&#8221; kombinasyonu arac\u0131l\u0131\u011f\u0131yla eri\u015febilirsiniz.<\/p>\n<p>Yukar\u0131daki kod blo\u011funa dikkat ederseniz e\u011fer server&#8217;da belirtti\u011fimiz &#8220;1234&#8221; portuna yani &#8220;http:\/\/localhost:1234&#8221; adresine bir connect ger\u00e7ekle\u015ftirilmektedir. Burada &#8220;io.connect()&#8221; fonksiyonuna &#8220;socket.io.js&#8221; k\u00fct\u00fcphanesi \u00fczerinden eri\u015filmektedir.<\/p>\n<p>\u015eimdi bu html sayfas\u0131n\u0131 a\u00e7t\u0131\u011f\u0131m\u0131z zaman, server&#8217;\u0131 takip eden socket devreye girecek ve a\u00e7\u0131k olan TCP ba\u011flant\u0131s\u0131 \u00fczerinden server&#8217;a mesaj g\u00f6nderecektir.<\/p>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/Node.js-\u2013-Websocket-Nedir-Nas\u0131l-Kullan\u0131l\u0131r.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/Node.js-\u2013-Websocket-Nedir-Nas\u0131l-Kullan\u0131l\u0131r.gif\" alt=\"Node.js \u2013 Websocket Nedir Nas\u0131l Kullan\u0131l\u0131r?\" width=\"600\" height=\"97\" class=\"aligncenter size-full wp-image-10082\" \/><\/a><br \/>\nG\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere&#8230;<\/p>\n<p>E\u011fer ki client ba\u011flant\u0131y\u0131 kestiyse yani sayfay\u0131 kapatt\u0131ysa &#8220;disconnect&#8221; olay\u0131 tetiklenecektir. Bu \u015fekilde server taraf\u0131ndan ilgili client&#8217;a ait ba\u011flant\u0131n\u0131n son buldu\u011funa dair bilgi edinilecektir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nio.sockets.on(&quot;connection&quot;, socket =&gt; {\r\n     console.log(&quot;User connection&quot;, socket.client.id);\r\n\r\n     socket.on(&quot;disconnect&quot;, () =&gt; {\r\n          console.log(&quot;User disconnect&quot;);\r\n     });\r\n});\r\n<\/pre>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/Node.js-\u2013-Websocket-Nedir-Nas\u0131l-Kullan\u0131l\u0131r-1.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/Node.js-\u2013-Websocket-Nedir-Nas\u0131l-Kullan\u0131l\u0131r-1.gif\" alt=\"Node.js \u2013 Websocket Nedir Nas\u0131l Kullan\u0131l\u0131r?\" width=\"600\" height=\"97\" class=\"aligncenter size-full wp-image-10085\" \/><\/a><br \/>\nYukar\u0131daki ekran g\u00f6r\u00fcnt\u00fcs\u00fcne dikkat ederseniz sayfay\u0131 yeniden y\u00fckledi\u011fimizde dahi \u00f6nce ba\u011flant\u0131y\u0131 kesecek ard\u0131ndan yeniden ba\u011flanacakt\u0131r. Bu detay\u0131da g\u00f6zden ka\u00e7\u0131rmaman\u0131z\u0131 \u00f6neririm&#8230;<\/p>\n<h3>Client ile Server&#8217;\u0131n Haberle\u015fmesi<\/h3>\n<p>Client ile server haberle\u015fmesinde temel esas, iki taraftan birinin &#8220;emit&#8221; fonksiyonu ile olay yaratmas\u0131 ve di\u011ferinin &#8220;on&#8221; fonksiyonu ile yarat\u0131lan olay\u0131 kar\u015f\u0131lamas\u0131 \u00fczerine kuruludur.<\/p>\n<p>\u00d6rne\u011fin;<br \/>\nClient&#8217;tan server&#8217;a haber g\u00f6nderilecekse;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n     &lt;script src=&quot;http:\/\/localhost:1234\/socket.io\/socket.io.js&quot;&gt;&lt;\/script&gt;\r\n     &lt;script&gt;\r\n          const socket = io.connect(&quot;http:\/\/localhost:1234&quot;);\r\n\r\n          socket.emit(&quot;sendMessageToServer&quot;);\r\n          \/\/Client'tan server'a mesaj g\u00f6nderirken burada &quot;sendMessageToServer&quot; ad\u0131nda bir olay yarat\u0131lmaktad\u0131r. Server'da bu olay\u0131 kar\u015f\u0131layacakt\u0131r.\r\n     &lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n     &lt;p&gt;\r\n          Merhaba, WebSocket\r\n     &lt;\/p&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>G\u00f6r\u00fcld\u00fc\u011f\u00fc gibi client&#8217;ta &#8220;emit&#8221; fonksiyonu ile &#8220;sendMessageToServer&#8221; ad\u0131nda bir olay yarat\u0131lmaktad\u0131r. \u015eimdi server&#8217;da bu olay\u0131 kar\u015f\u0131layal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nio.sockets.on(&quot;connection&quot;, socket =&gt; {\r\n     console.log(&quot;User connection&quot;, socket.client.id);\r\n\r\n     socket.on(&quot;disconnect&quot;, () =&gt; {\r\n          console.log(&quot;User disconnect&quot;);\r\n     });\r\n\r\n     socket.on(&quot;sendMessageToServer&quot;, () =&gt; {\r\n          console.log(&quot;sendMessageToServer tetiklendi.&quot;);\r\n     });\r\n});\r\n<\/pre>\n<p>G\u00f6rd\u00fc\u011f\u00fcn\u00fcz \u00fczere server&#8217;da ise &#8220;on&#8221; fonksiyonu ile &#8220;sendMessageToServer&#8221; olay\u0131 kar\u015f\u0131lanm\u0131\u015ft\u0131r.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/Node.js-\u2013-Websocket-Nedir-Nas\u0131l-Kullan\u0131l\u0131r-2.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/Node.js-\u2013-Websocket-Nedir-Nas\u0131l-Kullan\u0131l\u0131r-2.gif\" alt=\"Node.js \u2013 Websocket Nedir Nas\u0131l Kullan\u0131l\u0131r?\" width=\"600\" height=\"97\" class=\"aligncenter size-full wp-image-10089\" \/><\/a><\/p>\n<p>Server&#8217;dan client&#8217;a haber g\u00f6nderilecekse;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nio.sockets.on(&quot;connection&quot;, socket =&gt; {\r\n     console.log(&quot;User connection&quot;, socket.client.id);\r\n\r\n     socket.on(&quot;disconnect&quot;, () =&gt; {\r\n          console.log(&quot;User disconnect&quot;);\r\n     });\r\n\r\n     socket.emit(&quot;sendMessageToClient&quot;);\r\n});\r\n<\/pre>\n<p>&#8220;sendMessageToClient&#8221; isimli bir olay olu\u015fturulmu\u015ftur. \u015eimdi client bu olaya &#8220;on&#8221; fonksiyonu ile ba\u011flanacakt\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n     &lt;script src=&quot;http:\/\/localhost:1234\/socket.io\/socket.io.js&quot;&gt;&lt;\/script&gt;\r\n     &lt;script&gt;\r\n          const socket = io.connect(&quot;http:\/\/localhost:1234&quot;);\r\n          socket.on(&quot;sendMessageToClient&quot;, () =&gt; {\r\n               \/\/Server'da olu\u015fturulan olay clientda on fonksiyonuyla kar\u015f\u0131lanm\u0131\u015ft\u0131r.\r\n               alert(&quot;sendMessageToClient tetiklendi.&quot;);\r\n          });\r\n     &lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n     &lt;p&gt;\r\n          Merhaba, WebSocket\r\n     &lt;\/p&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/Node.js-\u2013-Websocket-Nedir-Nas\u0131l-Kullan\u0131l\u0131r-3.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/Node.js-\u2013-Websocket-Nedir-Nas\u0131l-Kullan\u0131l\u0131r-3.gif\" alt=\"Node.js \u2013 Websocket Nedir Nas\u0131l Kullan\u0131l\u0131r?\" width=\"600\" height=\"97\" class=\"aligncenter size-full wp-image-10091\" \/><\/a><\/p>\n<p>Burada son kez hat\u0131rlatma mahiyetinde HTTP protokol\u00fc de\u011fil websocket kulland\u0131\u011f\u0131m\u0131zdan dolay\u0131 istek de\u011fil mesaj g\u00f6ndermekteyiz ve her g\u00f6nderilen mesaj neticesinde cevap almak zorunda de\u011filiz.<\/p>\n<h3>Haberle\u015fme Esnas\u0131nda Parametre G\u00f6nderme<\/h3>\n<p>Haberle\u015fme s\u00fcrecinde iki tarafta birbirlerine parametre g\u00f6nderebilirler. Tabi bunun i\u00e7in olay yaratan taraf\u0131n yani &#8220;emit&#8221; fonksiyonunu kullanan taraf\u0131n ilgili fonksiyona ikinci parametreyi vermesi yeterlidir.<\/p>\n<p>\u015e\u00f6yle ki;<br \/>\nClient&#8217;tan, server&#8217;a parametre g\u00f6nderilecekse e\u011fer;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n     &lt;script src=&quot;http:\/\/localhost:1234\/socket.io\/socket.io.js&quot;&gt;&lt;\/script&gt;\r\n     &lt;script&gt;\r\n          const socket = io.connect(&quot;http:\/\/localhost:1234&quot;);\r\n\r\n          socket.emit(&quot;sendMessageToServer&quot;, 5);\r\n     &lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n     &lt;p&gt;\r\n          Merhaba, WebSocket\r\n     &lt;\/p&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>G\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere &#8220;emit&#8221; fonksiyonunun ikinci parametresine server&#8217;a g\u00f6nderilecek parametrenin de\u011feri verilmektedir. Server&#8217;\u0131n bu parametre de\u011ferini yakalayabilmesi i\u00e7in &#8220;on&#8221; fonksiyonunun callback fonksiyonuna belirtece\u011fi parametre i\u015f g\u00f6recektir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nio.sockets.on(&quot;connection&quot;, socket =&gt; {\r\n     console.log(&quot;User connection&quot;, socket.client.id);\r\n\r\n     socket.on(&quot;disconnect&quot;, () =&gt; {\r\n          console.log(&quot;User disconnect&quot;);\r\n     });\r\n\r\n     socket.on(&quot;sendMessageToServer&quot;, data =&gt; console.log(&quot;G\u00f6nderilen parametre : &quot; + data));\r\n});\r\n<\/pre>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/Node.js-\u2013-Websocket-Nedir-Nas\u0131l-Kullan\u0131l\u0131r-4.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/Node.js-\u2013-Websocket-Nedir-Nas\u0131l-Kullan\u0131l\u0131r-4.gif\" alt=\"Node.js \u2013 Websocket Nedir Nas\u0131l Kullan\u0131l\u0131r?\" width=\"600\" height=\"98\" class=\"aligncenter size-full wp-image-10097\" \/><\/a><\/p>\n<p>Benzer mant\u0131kla;<br \/>\nServer&#8217;dan, client&#8217;a parametre g\u00f6nderilecekse e\u011fer;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nio.sockets.on(&quot;connection&quot;, socket =&gt; {\r\n     console.log(&quot;User connection&quot;, socket.client.id);\r\n\r\n     socket.on(&quot;disconnect&quot;, () =&gt; {\r\n          console.log(&quot;User disconnect&quot;);\r\n     });\r\n\r\n     socket.emit(&quot;sendMessageToClient&quot;, 5);\r\n});\r\n<\/pre>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n     &lt;script src=&quot;http:\/\/localhost:1234\/socket.io\/socket.io.js&quot;&gt;&lt;\/script&gt;\r\n     &lt;script&gt;\r\n          const socket = io.connect(&quot;http:\/\/localhost:1234&quot;);\r\n\r\n          socket.emit(&quot;sendMessageToServer&quot;, 5);\r\n          socket.on(&quot;sendMessageToClient&quot;, data =&gt; alert(&quot;G\u00f6nderilen parametre : &quot; + data));\r\n     &lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n     &lt;p&gt;\r\n          Merhaba, WebSocket\r\n     &lt;\/p&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/Node.js-\u2013-Websocket-Nedir-Nas\u0131l-Kullan\u0131l\u0131r.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/Node.js-\u2013-Websocket-Nedir-Nas\u0131l-Kullan\u0131l\u0131r.png\" alt=\"Node.js \u2013 Websocket Nedir Nas\u0131l Kullan\u0131l\u0131r?\" width=\"747\" height=\"256\" class=\"aligncenter size-full wp-image-10099\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/Node.js-\u2013-Websocket-Nedir-Nas\u0131l-Kullan\u0131l\u0131r.png 747w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/12\/Node.js-\u2013-Websocket-Nedir-Nas\u0131l-Kullan\u0131l\u0131r-300x103.png 300w\" sizes=\"auto, (max-width: 747px) 100vw, 747px\" \/><\/a><\/p>\n<p>Bu sat\u0131rdan itibaren, Node.js&#8217;de ger\u00e7ek zamanl\u0131 uygulamalar\u0131n websocket ile nas\u0131l temellendirildi\u011fine ve client ile server aras\u0131ndaki haberle\u015fmenin nas\u0131l yap\u0131ld\u0131\u011f\u0131na dair teferruatl\u0131 de\u011finmi\u015f bulunmaktay\u0131z. Sonraki yaz\u0131lar\u0131m\u0131zda Websocket kullan\u0131m\u0131na dair daha detayl\u0131 i\u00e7erikler ile kar\u015f\u0131n\u0131zda olaca\u011f\u0131m.<\/p>\n<p>O halde \u015fimdilik g\u00f6r\u00fc\u015fmek \u00fczere diyelim&#8230;<\/p>\n<p>\u0130lgilenenlerin faydalanmas\u0131 dile\u011fiyle&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Bundan y\u0131llar y\u0131llar \u00f6ncesine kadar(yani ge\u00e7en y\u0131la kadar) web yap\u0131lanmalar\u0131nda ger\u00e7ek zamanl\u0131 i\u015fler yap\u0131laca\u011f\u0131 zaman Ajax teknolojisi kullan\u0131larak, belli aral\u0131klarla 1 saniye &#8211; milisaniye yap\u0131lan sorgular neticesinde sanki ger\u00e7ek zamanl\u0131 bir sistem izlenimi&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":8600,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2145],"tags":[2716,2717,645,2147,2720,2715,2721,2719,2713,2714,2718],"class_list":["post-10046","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js","tag-acknowledgement","tag-bi-directional-message","tag-long-polling","tag-node-js","tag-node-js-using-websocket","tag-node-js-websocket","tag-node-js-websocket-kullanimi","tag-socket-io-js","tag-websocket","tag-websocket-tcp","tag-xhr"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/10046","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=10046"}],"version-history":[{"count":48,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/10046\/revisions"}],"predecessor-version":[{"id":10102,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/10046\/revisions\/10102"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/8600"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=10046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=10046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=10046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}