{"id":1430,"date":"2023-09-08T05:00:00","date_gmt":"2023-09-08T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1430"},"modified":"2023-09-06T14:31:32","modified_gmt":"2023-09-06T21:31:32","slug":"rxjs-websockets-for-2-way-communication","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/rxjs-websockets-for-2-way-communication\/","title":{"rendered":"RxJs Websockets for 2-way communication"},"content":{"rendered":"\n<p>Earlier this year, I covered the <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/subject-behaviorsubject-and-replaysubject\/\" target=\"_blank\" rel=\"noopener\" title=\"\">different types of RxJs subjects<\/a>. There is another kind of Subject that I haven&#8217;t touched on yet: <code><a href=\"https:\/\/rxjs.dev\/api\/webSocket\/WebSocketSubject\" target=\"_blank\" rel=\"noopener\" title=\"\">WebSocketSubject<\/a><\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a websocket?<\/h2>\n\n\n\n<p>It&#8217;s a mechanism to establish and maintain a two-way connection between a client and a server on the web. This means that with websockets you can:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Receive real-time updates from a server <\/li>\n\n\n\n<li>Send real-time updates to a server<\/li>\n<\/ul>\n\n\n\n<p>There are several different use cases for websockets, such as: Real-time chat applications, online gaming, stock trading, and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Internet_of_things\" target=\"_blank\" rel=\"noopener\" title=\"\">IoT applications<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to create a websocket with RxJs?<\/h2>\n\n\n\n<p>All we need is the <code><a href=\"https:\/\/rxjs.dev\/api\/webSocket\/webSocket\" target=\"_blank\" rel=\"noopener\" title=\"\">webSocket<\/a><\/code> function from RxJs:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"165\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-7-1024x165.png\" alt=\"\" class=\"wp-image-1431\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-7-1024x165.png 1024w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-7-300x48.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-7-768x124.png 768w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-7-1536x247.png 1536w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-7.png 1714w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Note that the protocol is <code>ws<\/code> and not <code>http<\/code>. The above example assumes that our server accepts Websocket connections on port 8000. You can find a small <a href=\"https:\/\/github.com\/alcfeoh\/ng2-training\/blob\/0bf0f0c1371bf717a38a2c10d0f0d3f56c2ac277\/posts-server.js#L2-L18\" target=\"_blank\" rel=\"noopener\" title=\"\">example of such server-side code written with Node.js here<\/a>.<\/p>\n\n\n\n<p>To receive data from a WebSocketSubject, all we have to do is subscribe to it:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"100\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-8-1024x100.png\" alt=\"\" class=\"wp-image-1432\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-8-1024x100.png 1024w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-8-300x29.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-8-768x75.png 768w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-8-1536x151.png 1536w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-8.png 1714w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>And if we want to send data to the server, we use the <code>.next()<\/code> method, which behaves differently from the one in the other types of Subjects:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"133\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-9-1024x133.png\" alt=\"\" class=\"wp-image-1433\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-9-1024x133.png 1024w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-9-300x39.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-9-768x99.png 768w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-9-1536x199.png 1536w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-9.png 1714w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Now you know how RxJs provides a straightforward implementation for handling Websockets using a specific type of Subject.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Earlier this year, I covered the different types of RxJs subjects. There is another kind of Subject that I haven&#8217;t touched on yet: WebSocketSubject. What is a websocket? It&#8217;s a mechanism to establish and maintain a two-way connection between a client and a server on the web. This means that with websockets you can: There [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-1430","post","type-post","status-publish","format-standard","hentry","category-rxjs"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1430","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1430"}],"version-history":[{"count":1,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1430\/revisions"}],"predecessor-version":[{"id":1434,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1430\/revisions\/1434"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}