﻿
{"id":10766,"date":"2019-04-15T13:28:25","date_gmt":"2019-04-15T13:28:25","guid":{"rendered":"https:\/\/www.gencayyildiz.com\/blog\/?p=10766"},"modified":"2019-04-15T13:28:25","modified_gmt":"2019-04-15T13:28:25","slug":"angular-7-socketiomodule-modulu-ile-websocket-islemleri","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/angular-7-socketiomodule-modulu-ile-websocket-islemleri\/","title":{"rendered":"Angular 7 &#8211; SocketIoModule Mod\u00fcl\u00fc \u0130le Websocket \u0130\u015flemleri"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Bu i\u00e7eri\u011fimizde <strong><em>Angular 7<\/em><\/strong> ile birlikte <em><strong>SocketIoModule<\/strong><\/em> mod\u00fcl\u00fcn\u00fc kullanarak bir websockete nas\u0131l ba\u011flan\u0131laca\u011f\u0131n\u0131 ve bu ba\u011flant\u0131 \u00fczerinden nas\u0131l isteklerde bulunulaca\u011f\u0131n\u0131 inceleyece\u011fiz.<\/p>\n<p>Makale boyunca backend olarak <a href=\"https:\/\/www.gencayyildiz.com\/blog\/node-js-websocket-nedir-nasil-kullanilir\/\" rel=\"noopener noreferrer\" target=\"_blank\">websocket sunucusunu Node.js<\/a> temelli in\u015fa etti\u011fimiz a\u015fa\u011f\u0131daki \u00e7al\u0131\u015fma kar\u015f\u0131layacakt\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst http = require(&quot;http&quot;);\r\nconst socket = require(&quot;socket.io&quot;);\r\nconst server = http.createServer((request, response) =&gt; response.end(&quot;...&quot;));\r\nconst io = socket.listen(server.listen(5000, () =&gt; console.log(&quot;Started...&quot;)));\r\nio.on(&quot;connect&quot;, socket =&gt; {\r\n    console.log(`${socket.id} connected.`);\r\n    socket.on(&quot;disconnect&quot;, () =&gt; console.log(`${socket.id} disconnected.`));\r\n    socket.broadcast.on(&quot;message&quot;, data =&gt; socket.broadcast.emit(&quot;message&quot;, data));\r\n});\r\n<\/pre>\n<h4>SocketIoModule Paketinin \u0130ndirilmesi<\/h4>\n<p>Herhangi bir Angular 7 uygulamas\u0131na SocketIoModule paketinin indirilebilmesi i\u00e7in Angular CLI taraf\u0131ndan a\u015fa\u011f\u0131daki kodun \u00e7al\u0131\u015ft\u0131r\u0131lmas\u0131 yeterlidir;<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nAngular CLI : npm install ng-socket-io --save\r\n<\/pre>\n<h4>Uygulamaya Entegrasyonu<\/h4>\n<p>\u0130ndirilen SocketIoModule paketini uygulamaya entegre edebilmek i\u00e7in &#8220;app.module.ts&#8221; dosyas\u0131na \u015fu;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { SocketIoModule, SocketIoConfig } from 'ng-socket-io';\r\nconst config: SocketIoConfig = { url: 'http:\/\/localhost:5000', options: {} };\r\n<\/pre>\n<p>ve \u015fu;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n  imports: &#x5B;\r\n    BrowserModule,\r\n    AppRoutingModule,\r\n    SocketIoModule.forRoot(config)\r\n  ],\r\n<\/pre>\n<p>kodlar\u0131n\u0131 yerle\u015ftirmemiz gerekmektedir. Tam bir \u00f6rnek isteyenler i\u00e7in a\u015fa\u011f\u0131ya b\u00fct\u00fcn olarak &#8220;app.module.ts&#8221; dosyas\u0131n\u0131 alal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { BrowserModule } from '@angular\/platform-browser';\r\nimport { NgModule } from '@angular\/core';\r\nimport { AppRoutingModule } from '.\/app-routing.module';\r\nimport { AppComponent } from '.\/app.component';\r\n\r\nimport { SocketIoModule, SocketIoConfig } from 'ng-socket-io';\r\nconst config: SocketIoConfig = { url: 'http:\/\/localhost:5000', options: {} };\r\n\r\n@NgModule({\r\n  declarations: &#x5B;\r\n    AppComponent\r\n  ],\r\n  imports: &#x5B;\r\n    BrowserModule,\r\n    AppRoutingModule,\r\n    SocketIoModule.forRoot(config)\r\n  ],\r\n  providers: &#x5B;],\r\n  bootstrap: &#x5B;AppComponent]\r\n})\r\nexport class AppModule { }\r\n<\/pre>\n<h4>Service Olu\u015fturma<\/h4>\n<p>Ard\u0131ndan websocket ba\u011flant\u0131s\u0131n\u0131 ger\u00e7ekle\u015ftirebilmesi ve t\u00fcm s\u00fcreci organize edebilmesi i\u00e7in bir service olu\u015ftural\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { Injectable } from '@angular\/core';\r\nimport { Socket } from 'ng-socket-io';\r\n\r\n@Injectable({\r\n  providedIn: 'root'\r\n})\r\nexport class SocketService {\r\n  constructor(private socket: Socket) { }\r\n  sendMessage(message: string) {\r\n    this.socket.emit(&quot;message&quot;, message);\r\n  }\r\n  getMessage() {\r\n    this.socket.on(&quot;message&quot;, data =&gt; alert(data));\r\n  }\r\n}\r\n<\/pre>\n<p>Yukar\u0131da tan\u0131mlanan service&#8217;e dikkat ederseniz e\u011fer biri mesaj g\u00f6ndermek i\u00e7in &#8220;sendMessage&#8221; ve bir di\u011feri ise g\u00f6nderilen mesajlar\u0131 almas\u0131 i\u00e7in &#8220;getMessage&#8221; olmak \u00fczere iki adet metot tan\u0131mlanm\u0131\u015ft\u0131r. Sizler burada ihtiyac\u0131n\u0131za d\u00f6n\u00fck farkl\u0131 operasyonel fonksiyonlar tan\u0131mlayabilirsiniz.<\/p>\n<p>Son olarak tan\u0131mlanan bu service&#8217;i &#8220;app.module.ts&#8221; dosyas\u0131nda provider olarak uygulamaya dahil etmeyi unutmuyoruz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n@NgModule({\r\n  .\r\n  .\r\n  .\r\n  providers: &#x5B;SocketService],\r\n  .\r\n  .\r\n  .\r\n})\r\n<\/pre>\n<h4>Olas\u0131 Hatalar\u0131n Temizlenmesi<\/h4>\n<p>Angular uygulamas\u0131n\u0131 yay\u0131na al\u0131rken s\u00fcre\u00e7 esnas\u0131nda olas\u0131 hatalarla kar\u015f\u0131la\u015fabilirsiniz. E\u011fer ki bu hatalardan biri;<\/p>\n<blockquote style=\"color:red;\"><p><em><strong>ERROR in node_modules\/ng-socket-io\/dist\/src\/socket-io.service.d.ts(1,10): error TS2305: Module &#8216;&#8221;\/node_modules\/rxjs\/Observable&#8221;&#8216; has no exported member &#8216;Observable&#8217;.<br \/>\nnode_modules\/rxjs\/Observable.d.ts(1,15): error TS2307: Cannot find module &#8216;rxjs-compat\/Observable&#8217;.<\/strong><\/em><\/p><\/blockquote>\n<p>\u015feklindeyse yapman\u0131z gereken <em>rxjs-compat<\/em> k\u00fct\u00fcphanesini projeye entegre etmenizdir.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nAngular CLI : npm i rxjs-compat\r\n<\/pre>\n<p>\u0130lgili paket yukar\u0131daki komut ile Angular CLI arac\u0131l\u0131\u011f\u0131yla uygulamaya dahil edildi\u011fi taktirde belirtilen olas\u0131 hata d\u00fczeltilmi\u015f olacakt\u0131r.<\/p>\n<p>Bu hatay\u0131 d\u00fczelttikten sonra uygulamay\u0131 yay\u0131na al\u0131rken a\u015fa\u011f\u0131daki hataylada kar\u015f\u0131la\u015f\u0131lma ihtimali y\u00fcksektir;<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/04\/Angular-7-\u2013-SocketIoModule-Mod\u00fcl\u00fc-\u0130le-Websocket-\u0130\u015flemleri.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/04\/Angular-7-\u2013-SocketIoModule-Mod\u00fcl\u00fc-\u0130le-Websocket-\u0130\u015flemleri.png\" alt=\"Angular 7 \u2013 SocketIoModule Mod\u00fcl\u00fc \u0130le Websocket \u0130\u015flemleri\" width=\"351\" height=\"50\" class=\"aligncenter size-full wp-image-10779\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/04\/Angular-7-\u2013-SocketIoModule-Mod\u00fcl\u00fc-\u0130le-Websocket-\u0130\u015flemleri.png 351w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/04\/Angular-7-\u2013-SocketIoModule-Mod\u00fcl\u00fc-\u0130le-Websocket-\u0130\u015flemleri-300x43.png 300w\" sizes=\"auto, (max-width: 351px) 100vw, 351px\" \/><\/a><br \/>\n\u0130lgili hatan\u0131n metinsel halinide a\u015fa\u011f\u0131ya al\u0131rsak e\u011fer;<\/p>\n<blockquote style=\"color:red;\"><p><em><strong>Uncaught ReferenceError: global is not defined<\/strong><\/em><\/p><\/blockquote>\n<p>\u015feklindedir.<\/p>\n<p>Bu hataya kar\u015f\u0131 \u00e7\u00f6z\u00fcm\u00fc ise a\u015fa\u011f\u0131daki kodu uygulaman\u0131n &#8220;polyfills.ts&#8221; dosyas\u0131n\u0131n makul bir noktas\u0131na ekleyerek ger\u00e7ekle\u015ftiriyoruz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n(window as any).global = window\r\n<\/pre>\n<h4>Aray\u00fcz Tasar\u0131m\u0131<\/h4>\n<p>\u015eimdide client&#8217;\u0131n kullanaca\u011f\u0131 aray\u00fcz\u00fc tasarlayal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;input type=&quot;text&quot; #txtMessage&gt; &lt;button (click)=&quot;Send(txtMessage.value)&quot;&gt;Send&lt;\/button&gt;\r\n<\/pre>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { Component } from '@angular\/core';\r\nimport { SocketService } from '.\/services\/socket.service';\r\n\r\n@Component({\r\n  selector: 'app-root',\r\n  templateUrl: '.\/app.component.html',\r\n  styleUrls: &#x5B;'.\/app.component.css']\r\n})\r\nexport class AppComponent {\r\n\r\n  constructor(private socket: SocketService) {\r\n    socket.getMessage();\r\n  }\r\n  Send(message: string) {\r\n    this.socket.sendMessage(message);\r\n  }\r\n}\r\n<\/pre>\n<h4>Uygulamay\u0131 Test Edelim<\/h4>\n<p>\u015eu ana kadar yapm\u0131\u015f oldu\u011fumuz \u00e7al\u0131\u015fma neticesinde uygulamay\u0131 aya\u011fa kald\u0131r\u0131p test edersek e\u011fer;<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/04\/Angular-7-\u2013-SocketIoModule-Mod\u00fcl\u00fc-\u0130le-Websocket-\u0130\u015flemleri.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/04\/Angular-7-\u2013-SocketIoModule-Mod\u00fcl\u00fc-\u0130le-Websocket-\u0130\u015flemleri.gif\" alt=\"Angular 7 \u2013 SocketIoModule Mod\u00fcl\u00fc \u0130le Websocket \u0130\u015flemleri\" width=\"600\" height=\"401\" class=\"aligncenter size-full wp-image-10792\" \/><\/a><br \/>\nyukar\u0131daki ekran g\u00f6r\u00fcnt\u00fcs\u00fcnde oldu\u011fu gibi sorunsuz \u00e7al\u0131\u015fmaktad\u0131r.<\/p>\n<p>Ve i\u015fte b\u00f6ylece Angular uygulamalar\u0131nda websocket \u00e7al\u0131\u015fmalar\u0131n\u0131n nas\u0131l yap\u0131ld\u0131\u011f\u0131n\u0131 incelemi\u015f olduk. Muhtemelen bir sonraki i\u00e7eri\u011fimizde ise Angular 7 ile birlikte birden fazla client\u0131 kapsayacak \u015fekilde websocket ba\u011flant\u0131s\u0131 \u00fczerinde \u00e7al\u0131\u015fan bir top oyunu yazaca\u011f\u0131z.<\/p>\n<p>O halde \u015fimdilik g\u00f6r\u00fc\u015fmek \u00fczere diyelim&#8230;<br \/>\n\u0130lgilenenlerin faydalanmas\u0131 dile\u011fiyle&#8230;<\/p>\n<p>\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, Bu i\u00e7eri\u011fimizde Angular 7 ile birlikte SocketIoModule mod\u00fcl\u00fcn\u00fc kullanarak bir websockete nas\u0131l ba\u011flan\u0131laca\u011f\u0131n\u0131 ve bu ba\u011flant\u0131 \u00fczerinden nas\u0131l isteklerde bulunulaca\u011f\u0131n\u0131 inceleyece\u011fiz. Makale boyunca backend olarak websocket sunucusunu Node.js temelli in\u015fa etti\u011fimiz a\u015fa\u011f\u0131daki \u00e7al\u0131\u015fma&#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":10794,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2144,2415,2599,2756,2145],"tags":[2757,2821,2823,2822,2696,2820,2713],"class_list":["post-10766","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular-4","category-angular-5","category-angular-6","category-angular-7","category-node-js","tag-angular-7","tag-angular-7-socketiomodule","tag-angular-7-websocket","tag-angular-websocket","tag-rxjs-compat","tag-socketiomodule","tag-websocket"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/10766","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=10766"}],"version-history":[{"count":24,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/10766\/revisions"}],"predecessor-version":[{"id":10795,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/10766\/revisions\/10795"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/10794"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=10766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=10766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=10766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}