{"id":5310,"date":"2024-08-20T08:35:00","date_gmt":"2024-08-20T06:35:00","guid":{"rendered":"https:\/\/viva.racunalniske-novice.com\/primer-implementacije-sprotnega-tekstovnega-pogovora-s-knjiznico-signalr\/"},"modified":"2024-08-20T08:35:00","modified_gmt":"2024-08-20T06:35:00","slug":"primer-implementacije-sprotnega-tekstovnega-pogovora-s-knjiznico-signalr","status":"publish","type":"post","link":"https:\/\/viva.racunalniske-novice.com\/en\/example-of-the-implementation-of-real-time-text-conversation-with-the-signalr-library\/","title":{"rendered":"An example of implementing a real-time text conversation with the SignalR library"},"content":{"rendered":"<p><em>Author: Denis Balant, Enej Hudobreznik<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>The HTTP (Hypertext Transfer Protocol) protocol is still primarily used for data transfer over the Internet. its more modern and safer encrypted derivative HTTPS (HTTP Secure). They are based on a client-server architecture, where the client (English client) sends a request (English HTTP request) to the server (English Server), and the server responds to it with a response (English HTTP response).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/08\/image.png\" alt=\"HTTP requests and responses (source: https:\/\/www.telerik.com\/blogs\/real-time-communication-techniques)\" class=\"wp-image-2538892\"\/><figcaption><sub>HTTP requests and responses (source: <a href=\"https:\/\/www.telerik.com\/blogs\/real-time-communication-techniques\">https:\/\/www.telerik.com\/blogs\/real-time-communication-techniques<\/a>)<\/sub><\/figcaption><\/figure>\n\n\n\n<p>HTTP and HTTPS are suitable for simple request-response transfers (e.g., downloading an HTML file at the request of a client). Using these two protocols, the user can only obtain updated data when a new request is made, so they are not best suited for real-time data transfer (e.g., online chat, online video games, location tracking, etc.), on which much of the functionality of modern web applications is based. There are many different and better ways to implement agility, each with its own advantages and disadvantages. <\/p>\n\n\n\n<p>The simplest method of implementation is regular polling, where the client repeatedly sends requests for new data to the server every polling interval, regardless of whether the content has changed at all. Despite its simplicity, this approach is not suitable for larger services where access to the server requires many clients, because a large amount of unnecessary and repeated requests can overload it and thus slow down the performance of the service. &nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/08\/image-2.png\" alt=\"\" class=\"wp-image-2538894\"\/><figcaption><sub>Current interrogation (source: <a href=\"https:\/\/www.telerik.com\/blogs\/real-time-communication-techniques\">https:\/\/www.telerik.com\/blogs\/real-time-communication-techniques<\/a>)<\/sub><\/figcaption><\/figure>\n\n\n\n<p>The improvement is achieved by introducing long-term polling, where the client sends requests to the server in the same way, but the server does not respond to them until it detects the presence of new data. The disadvantage of such an approach is that the client must have a certain time interval defined, after which it recognizes the server&#039;s inactivity as an error. This adds an extra layer of complexity to the client and server setup.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/08\/image-3.png\" alt=\"\" class=\"wp-image-2538896\"\/><figcaption><sub>Prolonged interrogation (source: <a href=\"https:\/\/www.telerik.com\/blogs\/real-time-communication-techniques\">https:\/\/www.telerik.com\/blogs\/real-time-communication-techniques<\/a>)<\/sub><\/figcaption><\/figure>\n\n\n\n<p>The modern HTML5 standard also provides an API called Server-Sent Events. It is a protocol where the client does not have to periodically send requests to the server, but the server resends the necessary data when changes are made, thus facilitating real-time communication.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/08\/image-4.png\" alt=\"\" class=\"wp-image-2538898\"\/><figcaption><sub>Server-Sent Events (source: <a href=\"https:\/\/www.telerik.com\/blogs\/real-time-communication-techniques\">https:\/\/www.telerik.com\/blogs\/real-time-communication-techniques<\/a>)<\/sub><\/figcaption><\/figure>\n\n\n\n<p>The HTML5 standard also offers the WebSockets protocol, which enables true two-way real-time communication. At the beginning of the establishment, a handshake is performed, where the client and the server agree on the set of standards that they will use. After a successful handshake, a permanent connection with a small delay is opened between the two participants. This protocol is especially useful when it comes to a point-to-point connection (a direct connection between a client and a server).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/08\/image-5-e1724134052479.png\" alt=\"\" class=\"wp-image-2538902\"\/><\/figure>\n\n\n\n<p>In addition to the classic client-server architecture, there are other solutions where the connection between clients does not flow through servers, but directly between clients that play the role of both client and server (these peer-to-peer networks). A popular protocol for real-time communication in such networks is WebRTC.<\/p>\n\n\n\n<p>In the .NET environment for real-time communication, we can use the open-source SignalR library, which is part of the ASP.NET Core web framework, so it is easy to add it to existing projects as an additional level of middleware when processing incoming requests. The library simulates method calls on the client from the server side and method calls on the server from the client side (Remote Procedure Call - RPC), but does not guarantee the appropriateness of the no. parameters and their types. From the server, we can call methods on all clients, on a specific group of clients, or on just one client.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/08\/image-6.png\" alt=\"\" class=\"wp-image-2538908\"\/><figcaption><sub>Diagram of how the SignalR library works (<a href=\"https:\/\/learn.microsoft.com\/en-us\/aspnet\/signalr\/overview\/getting-started\/introduction-to-signalr\">https:\/\/learn.microsoft.com\/en-us\/aspnet\/signalr\/overview\/getting-started\/introduction-to-signalr<\/a>)<\/sub><\/figcaption><\/figure><\/div>\n\n\n\n<p>Its main advantage is the simplicity of implementation thanks to the software development packages (SDK) for many different platforms, which hide the inner workings of the service from the programmers, and the possibility of easy hosting with the Azure SignalR service. An additional advantage is that the service itself chooses the most appropriate connection type (described above) between the client and the server.<\/p>\n\n\n\n<p>The use of the library is based on special classes called hubs, which represent an abstraction of the connection between clients and the server. For them, we define methods that clients can call on the server.<\/p>\n\n\n\n<p>The example below shows an example of a simple text conversation implementation. First, we define the base node for the conversation on the server. The methods of this class represent methods that can be called by clients on the server. When the SendMessage method is called, the client sends the user ID (userId) and the content of the message (message), while the server calls the ReceiveMessage method on all clients with the specified parameters.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/08\/image-7.png\" alt=\"\" class=\"wp-image-2538942\"\/><figcaption><sub>Implementation of text conversation on the server in the .NET environment (own source)<\/sub><\/figcaption><\/figure>\n\n\n\n<p>The example below shows a JavaScript web client implementation using the official library (@microsoft\/signalr). Clients connect to the node URL (\/chat in the example below). A connection is represented by the connection object. The on method represents a listener to the method call, the name of which is given as the first parameter, and as the second the function that is executed on the event. We implement methods on the server through the invoke method of the connection object, which requires the name of the method as the first argument, followed by its parameters.<\/p>\n\n\n\n<p>By default, data is transferred between the server and clients in JSON format, but the library also supports the more efficient MessagePack format.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/08\/image-8.png\" alt=\"\" class=\"wp-image-2538944\"\/><figcaption><sub>Implementation of the web client in JavaScript language (own source)<\/sub><\/figcaption><\/figure>\n\n\n\n<p>Although the library greatly facilitates development, it has its drawbacks. One of the main ones is the scalability of the system for self-hosting and ensuring uninterrupted operation, since its implementation is very difficult to distribute on several separate servers. This turns out to be especially difficult if we want to reduce latency for remote users and host in several different locations, as the solution design is limited to just one. Also, the library does not ensure reliable delivery and order of messages, which can degrade the user experience.<\/p>","protected":false},"excerpt":{"rendered":"<p>Authors: Denis Balant, Enej Hudobreznik The HTTP (Hypertext Transfer Protocol) protocol or its more modern and secure encrypted derivative HTTPS (HTTP Secure) is still primarily used for data transfer over the Internet. They are based on the client-server architecture, where the client sends a request (HTTP request) to the server, and the server responds with a response [\u2026]<\/p>","protected":false},"author":2,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[66],"tags":[],"class_list":["post-5310","post","type-post","status-publish","format-standard","hentry","category-programi"],"acf":{"subtitle":"Kljub nenehnemu razvoju spletnih strani so tehnologije v ozadju skozi \u010das ostale zelo podobne. Ker splet ni dom le sodobnim stranem, ampak tudi starej\u0161im, ki morajo delovati tudi v sodobnih brskalnikih, so dana\u0161nje tehnologije pogosto le izbolj\u0161ane in dopolnjene razli\u010dic tistih, ki so nastale \u017ee ob samem za\u010detku svetovnega spleta. Naprednej\u0161e sodobne funkcije, ki jih te re\u0161itve ne podpirajo, pa omogo\u010dajo novi standardi.","heading":"","summary":"Kljub nenehnemu razvoju spletnih strani so tehnologije v ozadju skozi \u010das ostale zelo podobne. ","thumbnail_small":"https:\/\/racunalniske-novice.com\/wp-content\/uploads\/2024\/08\/coding-560x315.jpg","thumbnail_large":"https:\/\/racunalniske-novice.com\/wp-content\/uploads\/2024\/08\/coding-1024x682.jpg","thumbnail_caption":"Fotografija je ilustrativna. (Foto: Pixabay)","gallery":"","video_gallery":null,"author":"","links":null,"sources":null,"skip_language":[]},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Primer implementacije sprotnega tekstovnega pogovora s knji\u017enico SignalR - Ra\u010dunalni\u0161ke novice<\/title>\n<meta name=\"description\" content=\"Kljub nenehnemu razvoju spletnih strani so tehnologije v ozadju skozi \u010das ostale zelo podobne.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/viva.racunalniske-novice.com\/en\/wp-json\/wp\/v2\/posts\/5310\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Primer implementacije sprotnega tekstovnega pogovora s knji\u017enico SignalR - Ra\u010dunalni\u0161ke novice\" \/>\n<meta property=\"og:description\" content=\"Avtorja: Denis Balant, Enej Hudobreznik Za prenos podatkov preko spletu se \u0161e danes primarno uporablja protokol HTTP (angl. Hypertext Transfer Protocol) oz. njegova sodobnej\u0161a in varnej\u0161a \u0161ifrirana izpeljanka HTTPS (angl. HTTP Secure). Osnovana sta na arhitekturi odjemalec-stre\u017enik, kjer odjemalec (angl. client) po\u0161lje stre\u017eniku (angl. Server) zahtevo (angl. HTTP request), stre\u017enik pa nanj odgovori z odzivom [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/viva.racunalniske-novice.com\/en\/example-of-the-implementation-of-real-time-text-conversation-with-the-signalr-library\/\" \/>\n<meta property=\"og:site_name\" content=\"Ra\u010dunalni\u0161ke novice\" \/>\n<meta property=\"article:published_time\" content=\"2024-08-20T06:35:00+00:00\" \/>\n<meta name=\"author\" content=\"sinusiks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"sinusiks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/viva.racunalniske-novice.com\/primer-implementacije-sprotnega-tekstovnega-pogovora-s-knjiznico-signalr\/\",\"url\":\"https:\/\/viva.racunalniske-novice.com\/primer-implementacije-sprotnega-tekstovnega-pogovora-s-knjiznico-signalr\/\",\"name\":\"Primer implementacije sprotnega tekstovnega pogovora s knji\u017enico SignalR - Ra\u010dunalni\u0161ke novice\",\"isPartOf\":{\"@id\":\"https:\/\/viva.racunalniske-novice.com\/en\/#website\"},\"datePublished\":\"2024-08-20T06:35:00+00:00\",\"dateModified\":\"2024-08-20T06:35:00+00:00\",\"author\":{\"@id\":\"https:\/\/viva.racunalniske-novice.com\/en\/#\/schema\/person\/afb62e36efa34516d50249517e4cdbb4\"},\"breadcrumb\":{\"@id\":\"https:\/\/viva.racunalniske-novice.com\/primer-implementacije-sprotnega-tekstovnega-pogovora-s-knjiznico-signalr\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/viva.racunalniske-novice.com\/primer-implementacije-sprotnega-tekstovnega-pogovora-s-knjiznico-signalr\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/viva.racunalniske-novice.com\/primer-implementacije-sprotnega-tekstovnega-pogovora-s-knjiznico-signalr\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/viva.racunalniske-novice.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Primer implementacije sprotnega tekstovnega pogovora s knji\u017enico SignalR\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/viva.racunalniske-novice.com\/en\/#website\",\"url\":\"https:\/\/viva.racunalniske-novice.com\/en\/\",\"name\":\"Ra\u010dunalni\u0161ke novice\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/viva.racunalniske-novice.com\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/viva.racunalniske-novice.com\/en\/#\/schema\/person\/afb62e36efa34516d50249517e4cdbb4\",\"name\":\"sinusiks\",\"sameAs\":[\"https:\/\/ml.racunalniske-novice.com\"],\"url\":\"https:\/\/viva.racunalniske-novice.com\/en\/author\/sinusiks\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Primer implementacije sprotnega tekstovnega pogovora s knji\u017enico SignalR - Ra\u010dunalni\u0161ke novice","description":"Kljub nenehnemu razvoju spletnih strani so tehnologije v ozadju skozi \u010das ostale zelo podobne.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/viva.racunalniske-novice.com\/en\/wp-json\/wp\/v2\/posts\/5310","og_locale":"en_US","og_type":"article","og_title":"Primer implementacije sprotnega tekstovnega pogovora s knji\u017enico SignalR - Ra\u010dunalni\u0161ke novice","og_description":"Avtorja: Denis Balant, Enej Hudobreznik Za prenos podatkov preko spletu se \u0161e danes primarno uporablja protokol HTTP (angl. Hypertext Transfer Protocol) oz. njegova sodobnej\u0161a in varnej\u0161a \u0161ifrirana izpeljanka HTTPS (angl. HTTP Secure). Osnovana sta na arhitekturi odjemalec-stre\u017enik, kjer odjemalec (angl. client) po\u0161lje stre\u017eniku (angl. Server) zahtevo (angl. HTTP request), stre\u017enik pa nanj odgovori z odzivom [&hellip;]","og_url":"https:\/\/viva.racunalniske-novice.com\/en\/example-of-the-implementation-of-real-time-text-conversation-with-the-signalr-library\/","og_site_name":"Ra\u010dunalni\u0161ke novice","article_published_time":"2024-08-20T06:35:00+00:00","author":"sinusiks","twitter_card":"summary_large_image","twitter_misc":{"Written by":"sinusiks","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/viva.racunalniske-novice.com\/primer-implementacije-sprotnega-tekstovnega-pogovora-s-knjiznico-signalr\/","url":"https:\/\/viva.racunalniske-novice.com\/primer-implementacije-sprotnega-tekstovnega-pogovora-s-knjiznico-signalr\/","name":"Primer implementacije sprotnega tekstovnega pogovora s knji\u017enico SignalR - Ra\u010dunalni\u0161ke novice","isPartOf":{"@id":"https:\/\/viva.racunalniske-novice.com\/en\/#website"},"datePublished":"2024-08-20T06:35:00+00:00","dateModified":"2024-08-20T06:35:00+00:00","author":{"@id":"https:\/\/viva.racunalniske-novice.com\/en\/#\/schema\/person\/afb62e36efa34516d50249517e4cdbb4"},"breadcrumb":{"@id":"https:\/\/viva.racunalniske-novice.com\/primer-implementacije-sprotnega-tekstovnega-pogovora-s-knjiznico-signalr\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/viva.racunalniske-novice.com\/primer-implementacije-sprotnega-tekstovnega-pogovora-s-knjiznico-signalr\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/viva.racunalniske-novice.com\/primer-implementacije-sprotnega-tekstovnega-pogovora-s-knjiznico-signalr\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/viva.racunalniske-novice.com\/en\/"},{"@type":"ListItem","position":2,"name":"Primer implementacije sprotnega tekstovnega pogovora s knji\u017enico SignalR"}]},{"@type":"WebSite","@id":"https:\/\/viva.racunalniske-novice.com\/en\/#website","url":"https:\/\/viva.racunalniske-novice.com\/en\/","name":"Ra\u010dunalni\u0161ke novice","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/viva.racunalniske-novice.com\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/viva.racunalniske-novice.com\/en\/#\/schema\/person\/afb62e36efa34516d50249517e4cdbb4","name":"sinusiks","sameAs":["https:\/\/ml.racunalniske-novice.com"],"url":"https:\/\/viva.racunalniske-novice.com\/en\/author\/sinusiks\/"}]}},"_links":{"self":[{"href":"https:\/\/viva.racunalniske-novice.com\/en\/wp-json\/wp\/v2\/posts\/5310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/viva.racunalniske-novice.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/viva.racunalniske-novice.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/viva.racunalniske-novice.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/viva.racunalniske-novice.com\/en\/wp-json\/wp\/v2\/comments?post=5310"}],"version-history":[{"count":0,"href":"https:\/\/viva.racunalniske-novice.com\/en\/wp-json\/wp\/v2\/posts\/5310\/revisions"}],"wp:attachment":[{"href":"https:\/\/viva.racunalniske-novice.com\/en\/wp-json\/wp\/v2\/media?parent=5310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/viva.racunalniske-novice.com\/en\/wp-json\/wp\/v2\/categories?post=5310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/viva.racunalniske-novice.com\/en\/wp-json\/wp\/v2\/tags?post=5310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}