{"id":14161,"date":"2023-10-23T08:54:09","date_gmt":"2023-10-23T08:54:09","guid":{"rendered":"https:\/\/positiwise.com\/blog\/?p=14161"},"modified":"2023-12-27T08:45:19","modified_gmt":"2023-12-27T08:45:19","slug":"websockets-in-asp-net-core","status":"publish","type":"post","link":"https:\/\/positiwise.com\/blog\/websockets-in-asp-net-core","title":{"rendered":"WebSockets in ASP.NET Core: How to Use Them"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong class=\"pw-highlight-text-color\"><em>Quick Overview:<\/em><\/strong><br>The blog helps you discover how to use WebSockets in ASP.NET Core with step-by-step guidance. It provides a complete procedure to configure a WebSocket server and client and test their connection. In addition, you will also discover the WebSockets functionality while handling multiple users in a real-time chatroom.<\/p><div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/positiwise.com\/blog\/net-core-vs-node-js\/#A_Run-Through_NET_Core_vs_Nodejs\" >A Run-Through: .NET Core vs Node.js<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/positiwise.com\/blog\/net-core-vs-node-js\/#NET_Core\" >.NET Core<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/positiwise.com\/blog\/net-core-vs-node-js\/#Pros_of_using_NET_Core\" >Pros of using .NET Core<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/positiwise.com\/blog\/net-core-vs-node-js\/#Nodejs\" >Node.js<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/positiwise.com\/blog\/net-core-vs-node-js\/#A_Deep_Dive_into_NET_Core_vs_Nodejs\" >A Deep Dive into .NET Core vs. Node.js<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/positiwise.com\/blog\/net-core-vs-node-js\/#Processing_Approach_and_Performance\" >Processing Approach and Performance<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/positiwise.com\/blog\/net-core-vs-node-js\/#Security\" >Security<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/positiwise.com\/blog\/net-core-vs-node-js\/#Scalability\" >Scalability<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/positiwise.com\/blog\/net-core-vs-node-js\/#Platform_Adaptability\" >Platform Adaptability<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/positiwise.com\/blog\/net-core-vs-node-js\/#Support_from_Community\" >Support from Community<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/positiwise.com\/blog\/net-core-vs-node-js\/#Comparison_Table_between_NET_Core_and_Nodejs\" >Comparison Table between .NET Core and Node.js<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/positiwise.com\/blog\/net-core-vs-node-js\/#Wrapping_Up_on_NET_Core_vs_Nodejs\" >Wrapping Up on .NET Core vs Node.js<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n\n<h2 class=\"wp-block-heading\">How To Use WebSockets in ASP.NET Core<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In modern web applications, WebSocket connections are pretty popular and highly used. It helps businesses align with digital landscape requirements and process real-time data from end-users. It also supports empowering productivity, output rate, and user experience.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are not using it, you should start using websockets in ASP.NET Core applications. It will support you in achieving the expected business results in this dynamic market. To learn about its implementation, explore the process provided further.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are WebSockets in ASP.NET Core Applications?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In ASP.NET Core applications, Web Sockets enable a protocol that establishes low overhead communication between the server and the client. It allows devices in server-client architecture to communicate constantly until either of them closes the connection.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In real-time applications, WebSockets serve a primary role, as they enable the server to receive constant updates of data. This kind of connection is bidirectional in nature and requires HTTP support for stability, performance, and speed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When To Implement WebSocket Connection Functionality In ASP.NET Core Project?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You should implement WebSockets in ASP.NET Web API App and other applications only if it aligns with the following requirements or use cases.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Development of a real-time application, like chatrooms, FinTech, or enterprise system.<\/li>\n\n\n\n<li>Building an online game application where the server receives constant client updates.<\/li>\n\n\n\n<li>When you need UI refresh without the user asking for it.<\/li>\n\n\n\n<li>For building chat applications for internal use, within the department, or a building.<\/li>\n\n\n\n<li>To create forex and trading systems for real-time stock and share trading.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Similarly, you can identify whether your requirements fall under these scenarios. For better clarification, consult with a <a href=\"https:\/\/positiwise.com\/technologies\/dot-net-development\">.NET development<\/a> company or <a href=\"https:\/\/positiwise.com\/hire\/hire-dot-net-core-developers\">hire .NET Core developers<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Insight To Web Sockets in ASP.NET Core Applications: A Practical Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here, we will learn about web sockets&#8217; functionality in an ASP.NET Core project. Once you undergo this tutorial, you will understand the fundamentals of WebSocket communication configuration for an ASP.NET Web API app.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To start, download, install, and open the latest version of Visual Studio IDE. We will be using the .NET 6.0 framework for this tutorial. You can use a higher version, but before implementation, check its support for the protocol.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This procedure divides into three major phases, as outlined below:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Phase 1: Server-Side Configuration<\/li>\n\n\n\n<li>Phase 2: Client-Side Configuration<\/li>\n\n\n\n<li>Phase 3: Testing the WebSockets Communication<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s start with the process.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Phase 1: Server-Side Configuration<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 1:<\/strong> In Visual Studio IDE, <strong><em>create an empty ASP.NET Core project using C#<\/em><\/strong> as its primary programming language.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">On every project setup screen, you will find the <strong><em>Next\/Create<\/em><\/strong> button in the lower right corner. After filling in the details and choosing the reliable option, click on that button to move further.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"921\" height=\"466\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/create-empty-asp-net-core-project.png\" alt=\"create empty asp net core project\" class=\"wp-image-14172\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/create-empty-asp-net-core-project.png 921w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/create-empty-asp-net-core-project-300x152.png 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/create-empty-asp-net-core-project-768x389.png 768w\" sizes=\"auto, (max-width: 921px) 100vw, 921px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 2:<\/strong> On the &#8220;<strong><em>Configure your new project<\/em><\/strong>&#8221; screen, fill in the project details per your requirements. For this tutorial, we are using &#8220;<strong><em>SimpleWS-Server<\/em><\/strong>&#8221; as the project and solution name.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"722\" height=\"424\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/configure-simplews-server-project.png\" alt=\"configure simplews server project\" class=\"wp-image-14170\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/configure-simplews-server-project.png 722w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/configure-simplews-server-project-300x176.png 300w\" sizes=\"auto, (max-width: 722px) 100vw, 722px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 3:<\/strong> On the &#8220;<strong><em>Additional information<\/em><\/strong>&#8221; screen, select <strong><em>.NET 6.0<\/em><\/strong> and leave the rest as specified. Ensure that all the settings match as in the below snippet.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"719\" height=\"360\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/additional-information-net-6.png\" alt=\"additional information net 6\" class=\"wp-image-14169\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/additional-information-net-6.png 719w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/additional-information-net-6-300x150.png 300w\" sizes=\"auto, (max-width: 719px) 100vw, 719px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 4:<\/strong> Once the project is generated, you will see all its associated files in the Solutions Explorer tab. Also, it will display a default Hello World code as below. You have to <strong><em>replace<\/em><\/strong> the <strong><em>app.Run()<\/em><\/strong> method <strong><em>to app.RunAsync()<\/em><\/strong>.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"146\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/replace-the-apprun-method-to-apprunasync.png\" alt=\"replace the apprun method to apprunasync\" class=\"wp-image-14173\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/replace-the-apprun-method-to-apprunasync.png 723w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/replace-the-apprun-method-to-apprunasync-300x61.png 300w\" sizes=\"auto, (max-width: 723px) 100vw, 723px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 5:<\/strong> Now, we will be adding the <strong><em>UseWebSockets()<\/em><\/strong> function and <strong><em>builder.WebHost.UserUrls()<\/em><\/strong> method.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The function will enable our application to use WebSocket communication. The method will aid in defining the port and URL for the server, which, in our case, is \u201c<strong><em>http:\/\/localhost:6969.<\/em><\/strong>\u201d<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"572\" height=\"200\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/add-usewebsockets-method.png\" alt=\"add usewebsockets method\" class=\"wp-image-14167\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/add-usewebsockets-method.png 572w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/add-usewebsockets-method-300x105.png 300w\" sizes=\"auto, (max-width: 572px) 100vw, 572px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 6:<\/strong> After the WebSocket function, configure the server\u2019s endpoint mapping. We will be using the <strong><em>app.Map()<\/em><\/strong> method with WebSocket route as \u201c<strong><em>ws.<\/em><\/strong>\u201d In addition, we\u2019ll also be adding a context in the same method.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"571\" height=\"221\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/add-ws-in-app-map-method.png\" alt=\"add ws in app map method\" class=\"wp-image-14168\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/add-ws-in-app-map-method.png 571w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/add-ws-in-app-map-method-300x116.png 300w\" sizes=\"auto, (max-width: 571px) 100vw, 571px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 7:<\/strong> The context in the <strong><em>app.The map ()<\/em><\/strong> method needs to be configured. Inside it, we will add the logic to accept\/allow only WebSockets communication.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"617\" height=\"395\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/context-websockets-iswebsocketrequest.png\" alt=\"context websockets iswebsocketrequest\" class=\"wp-image-14171\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/context-websockets-iswebsocketrequest.png 617w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/context-websockets-iswebsocketrequest-300x192.png 300w\" sizes=\"auto, (max-width: 617px) 100vw, 617px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">For this, you have to <strong><em>add context.WebSockets.IsWebSocketRequest<\/em><\/strong> check. If the request is of a web socket, the ASP.NET Core application will accept it; otherwise, error 400 (Bad request) will be sent.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 8:<\/strong> Once the application confirms that it\u2019s a web socket connection, it should define an instance for it. To do so, add the <strong><em>below code<\/em><\/strong>.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>using var ws = await context.WebSockets.AcceptWebSocketAsync();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The instance will help verify the current connection state using the <strong><em>ws.State<\/em><\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"297\" height=\"233\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/public-enum-websocketstate.png\" alt=\"public enum websocketstate\" class=\"wp-image-14177\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In the above snippet, you can see all seven connection states that a web socket can undergo. Every state has a corresponding value used to determine the current state.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 9:<\/strong> To enable your server to send the messages, utilize the <strong><em>SendAsync()<\/em><\/strong> method on the WebSocket instance. It accepts the following parameters.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ArraySegment&lt;byte&gt;<\/li>\n\n\n\n<li>WebSocketMessageType enum<\/li>\n\n\n\n<li>Bool endofMessage<\/li>\n\n\n\n<li>CancelationToken<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">All these parameters are needed to convert as a byte array, leading to generating an <strong><em>ArraySegment&lt;byte&gt;<\/em><\/strong> and sending a message to the client.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var message = \"Hello World!\";\nvar bytes = Encoding.UTF8.GetBytes(message);\nvar arraySegment = new ArraySegment&lt;byte&gt;(bytes, 0, bytes.Length);\nawait ws.SendAsync(arraySegment,\n                    WebSocketMessageType.Text,\n                    true,\n                    CancellationToken.None);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 10:<\/strong> To enable the WebSocket server to return the current time to the client in &#8220;<strong><em>HH:MM:SS<\/em><\/strong>&#8221; format, wrap the below logic in a while(true) loop as below. The logic also uses a <strong><em>Thread.Sleep(1000)<\/em><\/strong> function for delaying the process by 1 second.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">In addition, the code will verify the WebSocket connection state only to send the message if the open state is detected.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"637\" height=\"587\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/verify-the-websocket-connection-state.png\" alt=\"verify the websocket connection state\" class=\"wp-image-14179\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/verify-the-websocket-connection-state.png 637w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/verify-the-websocket-connection-state-300x276.png 300w\" sizes=\"auto, (max-width: 637px) 100vw, 637px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the server-side configuration completes. Therefore, our WebSockets server is ready.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Phase 2: Client-Side Configuration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s create the WebSocket client.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 1:<\/strong> Open your Visual IDE to <strong><em>create a .NET Console app<\/em><\/strong> with C# as its programming language.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"716\" height=\"215\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/console-app.png\" alt=\"console app\" class=\"wp-image-14181\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/console-app.png 716w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/console-app-300x90.png 300w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 2:<\/strong> Complete the project details. For this tutorial, we are using <strong><em>SimpleWS-Client<\/em><\/strong> as a solution and project name.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"410\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/simplews-client-project.png\" alt=\"simplews client project\" class=\"wp-image-14188\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/simplews-client-project.png 720w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/simplews-client-project-300x171.png 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 3:<\/strong> <strong><em>Choose the .NET 6.0 framework<\/em><\/strong> and create the project.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"716\" height=\"264\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/select-net-6-as-framework.png\" alt=\"select net 6 as framework\" class=\"wp-image-14187\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/select-net-6-as-framework.png 716w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/select-net-6-as-framework-300x111.png 300w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 4:<\/strong> To enable a client device to initiate a web socket connection, we&#8217;ll use the <strong><em>ClientWebSocket<\/em><\/strong> provided by <strong><em>System.Net.WebScokets<\/em><\/strong>.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Net.WebSockets;\nvar ws = new ClientWebSocket();<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 5:<\/strong> For connecting with a WebSockets server, use the following code block. It comprises the <strong><em>ConnectAsync<\/em><\/strong> method with a cancelation token and URL.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>await ws.ConnectAsync(new Uri(\"ws:\/\/localhost:6969\/ws\"), \n  CancellationToken.None);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 6:<\/strong> Write the <strong><em>code for the client<\/em><\/strong> to receive messages from the server after the connection status changes to alive.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1275\" height=\"620\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receive-message-from-server.png\" alt=\"receive message from server\" class=\"wp-image-14184\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receive-message-from-server.png 1275w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receive-message-from-server-300x146.png 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receive-message-from-server-1024x498.png 1024w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receive-message-from-server-768x373.png 768w\" sizes=\"auto, (max-width: 1275px) 100vw, 1275px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 7:<\/strong> For receiving the messages from the WebSockets server, we&#8217;ll add the <strong><em>RecieveAsync()<\/em><\/strong> method to our client codebase. It will enable the client devices to accept cancellation tokens and <strong><em>ArraySegment&lt;byte><\/em><\/strong>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Also, you will be creating a byte array buffer for generating the ArraySegment&lt;byte&gt;.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1571\" height=\"445\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receiveasync-method.png\" alt=\"receiveasync method\" class=\"wp-image-14185\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receiveasync-method.png 1571w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receiveasync-method-300x85.png 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receiveasync-method-1024x290.png 1024w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receiveasync-method-768x218.png 768w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receiveasync-method-1536x435.png 1536w\" sizes=\"auto, (max-width: 1571px) 100vw, 1571px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 8:<\/strong> With all the above code, our result would still be in byte array format. However, we want it to display as a string. So, we have to implement encoding using the <strong><em>Encoding.UTF8.GetString()<\/em><\/strong> method.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1712\" height=\"686\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/encoding-utf8-getstring-method.png\" alt=\"encoding utf8 getstring method\" class=\"wp-image-14182\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/encoding-utf8-getstring-method.png 1712w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/encoding-utf8-getstring-method-300x120.png 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/encoding-utf8-getstring-method-1024x410.png 1024w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/encoding-utf8-getstring-method-768x308.png 768w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/encoding-utf8-getstring-method-1536x615.png 1536w\" sizes=\"auto, (max-width: 1712px) 100vw, 1712px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Additionally, add the below code to check whether the connection is not closed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>result.MessageType == WebSocketMessageType.Close<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 9:<\/strong> <strong><em>Check the code<\/em><\/strong>. Ensure that your final client-side code looks the same as the following.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1269\" height=\"762\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/final-code-websocket-client.png\" alt=\"final code websocket client\" class=\"wp-image-14183\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/final-code-websocket-client.png 1269w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/final-code-websocket-client-300x180.png 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/final-code-websocket-client-1024x615.png 1024w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/final-code-websocket-client-768x461.png 768w\" sizes=\"auto, (max-width: 1269px) 100vw, 1269px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 10:<\/strong> <strong><em>Test the functioning<\/em><\/strong> of the WebSockets server and client. First, run the server project and then the client project. The configuration is successful if it shows the current time in HH:MM:SS format.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"882\" height=\"503\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/run-websocket-server-and-client.png\" alt=\"run websocket server and client\" class=\"wp-image-14186\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/run-websocket-server-and-client.png 882w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/run-websocket-server-and-client-300x171.png 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/run-websocket-server-and-client-768x438.png 768w\" sizes=\"auto, (max-width: 882px) 100vw, 882px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Till now, we have tested the WebSockets project for one client. But, in real-time scenarios, there can be multiple end-users. So, in the next phase, we are going to test it for handling more than one WebSocket communication.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Phase 3: Testing the WebSockets Communication<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To test the multiple WebSockets connection, we are going to create and use a chatroom application. It will consist of the following functionalities:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Connecting multiple users and creating instances for them.<\/li>\n\n\n\n<li>Asking each user to enter their username\/name.<\/li>\n\n\n\n<li>Broadcasting the chatroom, joining and leaving notifications.<\/li>\n\n\n\n<li>Receiving and sending of messages by each user in the chatroom.<\/li>\n\n\n\n<li>Removal of client instance by checking the WebSocket connection state.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Similar to the configuration in the previous two phases, we will first write the code for the server and then for the client.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Server Code:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Your <strong><em>complete server code<\/em><\/strong> will be the following codebase.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"691\" height=\"851\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/complete-server-code.png\" alt=\"complete server code\" class=\"wp-image-14192\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/complete-server-code.png 691w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/complete-server-code-244x300.png 244w\" sizes=\"auto, (max-width: 691px) 100vw, 691px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s break down the code. First, you will find the code purpose and, below it, the part of the code performing that process.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For <strong><em>tracking multiple WebSocket<\/em><\/strong> connections.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"549\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/track-of-multiple-websocket-connections.png\" alt=\"track of multiple websocket connections\" class=\"wp-image-14197\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/track-of-multiple-websocket-connections.png 1200w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/track-of-multiple-websocket-connections-300x137.png 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/track-of-multiple-websocket-connections-1024x468.png 1024w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/track-of-multiple-websocket-connections-768x351.png 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To broadcast the message to each end-user\/client, the <strong><em>SendAsync()<\/em><\/strong> method is used.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1412\" height=\"592\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/broadcast-message-to-every-client.png\" alt=\"broadcast message to every client\" class=\"wp-image-14191\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/broadcast-message-to-every-client.png 1412w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/broadcast-message-to-every-client-300x126.png 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/broadcast-message-to-every-client-1024x429.png 1024w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/broadcast-message-to-every-client-768x322.png 768w\" sizes=\"auto, (max-width: 1412px) 100vw, 1412px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">For emulating chatroom and <strong><em>broadcasting the username<\/em><\/strong> of a newly connected client.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1097\" height=\"713\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/emulating-chatroom.png\" alt=\"emulating chatroom\" class=\"wp-image-14193\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/emulating-chatroom.png 1097w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/emulating-chatroom-300x195.png 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/emulating-chatroom-1024x666.png 1024w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/emulating-chatroom-768x499.png 768w\" sizes=\"auto, (max-width: 1097px) 100vw, 1097px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To manage the message-receiving process, the <strong><em>RecieveAsync() method<\/em><\/strong>, similar to client configuration, will be used.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1354\" height=\"445\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/function-receivemessage.png\" alt=\"function receivemessage\" class=\"wp-image-14194\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/function-receivemessage.png 1354w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/function-receivemessage-300x99.png 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/function-receivemessage-1024x337.png 1024w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/function-receivemessage-768x252.png 768w\" sizes=\"auto, (max-width: 1354px) 100vw, 1354px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Calling of ReceiveMessage() function inside <strong><em>WS handling<\/em><\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1364\" height=\"729\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receivemessage-function-inside-ws-handling.png\" alt=\"receivemessage function inside ws handling\" class=\"wp-image-14195\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receivemessage-function-inside-ws-handling.png 1364w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receivemessage-function-inside-ws-handling-300x160.png 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receivemessage-function-inside-ws-handling-1024x547.png 1024w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receivemessage-function-inside-ws-handling-768x410.png 768w\" sizes=\"auto, (max-width: 1364px) 100vw, 1364px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">For broadcasting the received messages in the chatroom, the ReceiveMessage() function is modified with <strong><em>Action&lt;WebSocketReceiveResult, byte[]&gt; handleMessage<\/em><\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1431\" height=\"524\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receivemessage-function-modified.png\" alt=\"receivemessage function modified\" class=\"wp-image-14196\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receivemessage-function-modified.png 1431w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receivemessage-function-modified-300x110.png 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receivemessage-function-modified-1024x375.png 1024w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/receivemessage-function-modified-768x281.png 768w\" sizes=\"auto, (max-width: 1431px) 100vw, 1431px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Further, the ReceiveMessage call is modified, <strong><em>enabling the lambda function<\/em><\/strong> to pass. In addition, within the lambda function, we will add the logic to broadcast messages from one user to all others present in the chatroom.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1096\" height=\"760\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/broadcast-received-text-message-inside-the-lambda-function.png\" alt=\"broadcast received text message inside the lambda function\" class=\"wp-image-14201\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/broadcast-received-text-message-inside-the-lambda-function.png 1096w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/broadcast-received-text-message-inside-the-lambda-function-300x208.png 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/broadcast-received-text-message-inside-the-lambda-function-1024x710.png 1024w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/broadcast-received-text-message-inside-the-lambda-function-768x533.png 768w\" sizes=\"auto, (max-width: 1096px) 100vw, 1096px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The code below is used to <strong><em>keep track of the users<\/em><\/strong> leaving the chatroom. It detects the WebSockets status to determine the presence of a client.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1152\" height=\"784\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/closeasync-method-to-close-connection.png\" alt=\"closeasync method to close connection\" class=\"wp-image-14200\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/closeasync-method-to-close-connection.png 1152w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/closeasync-method-to-close-connection-300x204.png 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/closeasync-method-to-close-connection-1024x697.png 1024w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/closeasync-method-to-close-connection-768x523.png 768w\" sizes=\"auto, (max-width: 1152px) 100vw, 1152px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Client Code:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">On the client side, add the following code block for <strong><em>inputting the username<\/em><\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1274\" height=\"434\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/inputting-the-username-client-side.png\" alt=\"inputting the username client side\" class=\"wp-image-14207\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/inputting-the-username-client-side.png 1274w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/inputting-the-username-client-side-300x102.png 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/inputting-the-username-client-side-1024x349.png 1024w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/inputting-the-username-client-side-768x262.png 768w\" sizes=\"auto, (max-width: 1274px) 100vw, 1274px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Use the below code for <strong><em>sending messages to the server<\/em><\/strong>, which will then be broadcast in the chatroom. Here, the SendAsync() method will be used on the ClientWebSocket instance.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1555\" height=\"463\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/sendasync-method-client-side.png\" alt=\"sendasync method client side\" class=\"wp-image-14209\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/sendasync-method-client-side.png 1555w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/sendasync-method-client-side-300x89.png 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/sendasync-method-client-side-1024x305.png 1024w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/sendasync-method-client-side-768x229.png 768w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/sendasync-method-client-side-1536x457.png 1536w\" sizes=\"auto, (max-width: 1555px) 100vw, 1555px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To <strong><em>manage the client<\/em><\/strong> exiting the chatroom, use the following code. It will tell the application to delete the WebSocket instance for that particular client.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1277\" height=\"318\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/delete-the-websocket-instance.png\" alt=\"delete the websocket instance\" class=\"wp-image-14206\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/delete-the-websocket-instance.png 1277w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/delete-the-websocket-instance-300x75.png 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/delete-the-websocket-instance-1024x255.png 1024w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/delete-the-websocket-instance-768x191.png 768w\" sizes=\"auto, (max-width: 1277px) 100vw, 1277px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Now, the client code is completed, and only the testing part is remaining.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To test the chatroom application, run the server and client projects. If you see the &#8220;<strong><em>user joined the room<\/em><\/strong>&#8221; message, the <a href=\"https:\/\/positiwise.com\/blog\/signalr-in-asp-net-core\">ASP.NET Core app<\/a> is running successfully.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"748\" height=\"255\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/user-joined-the-room.png\" alt=\"user joined the room\" class=\"wp-image-14210\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/user-joined-the-room.png 748w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/user-joined-the-room-300x102.png 300w\" sizes=\"auto, (max-width: 748px) 100vw, 748px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Now, <strong><em>create another client instance<\/em><\/strong> to verify whether the server is detecting and broadcasting new user-joined notifications. The output will look similar to the following.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"765\" height=\"561\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/create-another-client-instance.png\" alt=\"create another client instance\" class=\"wp-image-14205\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/create-another-client-instance.png 765w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/create-another-client-instance-300x220.png 300w\" sizes=\"auto, (max-width: 765px) 100vw, 765px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Further, <strong><em>send the message from the second instance<\/em><\/strong> and keep monitoring the first client to receive a message.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"760\" height=\"470\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/send-message-from-second-instance.png\" alt=\"send message from second instance\" class=\"wp-image-14208\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/send-message-from-second-instance.png 760w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/send-message-from-second-instance-300x186.png 300w\" sizes=\"auto, (max-width: 760px) 100vw, 760px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Now, <strong><em>close the connection<\/em><\/strong> for the second instance and <strong><em>open a new client<\/em><\/strong> (third client).<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"762\" height=\"739\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/close-connection-for-second-instance.png\" alt=\"close connection for second instance\" class=\"wp-image-14204\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/close-connection-for-second-instance.png 762w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/close-connection-for-second-instance-300x291.png 300w\" sizes=\"auto, (max-width: 762px) 100vw, 762px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Similarly, you can send messages, add new clients, remove current clients, and broadcast messages to test the ASP.NET Core WebSockets application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Concluding Up on WebSockets in ASP.NET Core<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Real-time applications extensively utilize WebSockets, offering bidirectional communication within the server-client architecture. To implement it, you should first choose the right use case, as mentioned in this blog. Further, you can follow the provided procedure to learn about its implementation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The WebSockets configuration guide enables exploring the functions and methods used on both the server and client side. You can create your own ASP.NET Core applications with WebSockets functionality by referencing them. However, if you want to develop enterprise software, you should contact <a href=\"https:\/\/positiwise.com\/hire\/asp-net-core-development-company\">ASP.NET Core Development Company<\/a><\/p>\n  <div class=\"related-post slider\">\r\n        <div class=\"headline\">Related Posts<\/div>\r\n    <div class=\"post-list owl-carousel\">\r\n\r\n            <div class=\"item\">\r\n            <div class=\"thumb post_thumb\">\r\n    <a  title=\".Net Core vs. Java \u2013 Which One You Should Use for Your Projects?\" href=\"https:\/\/positiwise.com\/blog\/net-core-vs-java-technology\">\r\n\r\n      <img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"800\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2021\/12\/web-security-partner-1.jpg\" class=\"attachment-full size-full wp-post-image\" alt=\".Net Core vs. Java \u2013 Which One You Should Use for Your Projects?\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2021\/12\/web-security-partner-1.jpg 1200w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2021\/12\/web-security-partner-1-300x200.jpg 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2021\/12\/web-security-partner-1-1024x683.jpg 1024w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2021\/12\/web-security-partner-1-768x512.jpg 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/>\r\n\r\n    <\/a>\r\n  <\/div>\r\n\r\n  <a class=\"title post_title\"  title=\".Net Core vs. Java \u2013 Which One You Should Use for Your Projects?\" href=\"https:\/\/positiwise.com\/blog\/net-core-vs-java-technology\">\r\n        .Net Core vs. Java \u2013 Which One You Should Use for Your Projects?  <\/a>\r\n\r\n        <\/div>\r\n              <div class=\"item\">\r\n            <div class=\"thumb post_thumb\">\r\n    <a  title=\"Precise Road Map for ASP.NET Developers\" href=\"https:\/\/positiwise.com\/blog\/roadmap-for-asp-dot-net-developers\">\r\n\r\n      <img loading=\"lazy\" decoding=\"async\" width=\"1021\" height=\"621\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2022\/01\/dealership-1200x800-1.jpg\" class=\"attachment-full size-full wp-post-image\" alt=\"Hire ASP.NET Developers - A Precise Road Map to Employ ASP.NET Developers\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2022\/01\/dealership-1200x800-1.jpg 1021w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2022\/01\/dealership-1200x800-1-300x182.jpg 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2022\/01\/dealership-1200x800-1-768x467.jpg 768w\" sizes=\"auto, (max-width: 1021px) 100vw, 1021px\" \/>\r\n\r\n    <\/a>\r\n  <\/div>\r\n\r\n  <a class=\"title post_title\"  title=\"Precise Road Map for ASP.NET Developers\" href=\"https:\/\/positiwise.com\/blog\/roadmap-for-asp-dot-net-developers\">\r\n        Precise Road Map for ASP.NET Developers  <\/a>\r\n\r\n        <\/div>\r\n              <div class=\"item\">\r\n            <div class=\"thumb post_thumb\">\r\n    <a  title=\".NET Core vs Node.js: The Difference Explained by Web Expert\" href=\"https:\/\/positiwise.com\/blog\/net-core-vs-node-js\">\r\n\r\n      <img loading=\"lazy\" decoding=\"async\" width=\"1021\" height=\"621\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2022\/01\/NET-Core-VS-Node-js-jpg.webp\" class=\"attachment-full size-full wp-post-image\" alt=\"NET-Core-VS-Node-js\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2022\/01\/NET-Core-VS-Node-js-jpg.webp 1021w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2022\/01\/NET-Core-VS-Node-js-300x182.webp 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2022\/01\/NET-Core-VS-Node-js-768x467.webp 768w\" sizes=\"auto, (max-width: 1021px) 100vw, 1021px\" \/>\r\n\r\n    <\/a>\r\n  <\/div>\r\n\r\n  <a class=\"title post_title\"  title=\".NET Core vs Node.js: The Difference Explained by Web Expert\" href=\"https:\/\/positiwise.com\/blog\/net-core-vs-node-js\">\r\n        .NET Core vs Node.js: The Difference Explained by Web Expert  <\/a>\r\n\r\n        <\/div>\r\n      \r\n  <\/div>\r\n\r\n  <script>\r\n      <\/script>\r\n  <style>\r\n    .related-post {}\r\n\r\n    .related-post .post-list {\r\n      text-align: left;\r\n          }\r\n\r\n    .related-post .post-list .item {\r\n      margin: 5px;\r\n      padding: 0px;\r\n          }\r\n\r\n    .related-post .headline {\r\n      font-size: 2rem !important;\r\n      color: #212529 !important;\r\n          }\r\n\r\n    .related-post .headline {\r\nmargin-top: 0;\r\npadding-left: 1%;\r\n}.related-post .post-list .item .post_thumb {\r\n      max-height: 220px;\r\n      margin: 0px 0px 10px 0px;\r\n      padding: 0px;\r\n      display: block;\r\n          }\r\n\r\n    .related-post .post-list .item .post_title {\r\n      font-size: 1rem;\r\n      color: #212529;\r\n      margin: 10px 0px;\r\n      padding: 0px;\r\n      display: block;\r\n      text-decoration: none;\r\n          }\r\n\r\n    .related-post .post-list .item .post_excerpt {\r\n      font-size: 13px;\r\n      color: #3f3f3f;\r\n      margin: 10px 0px;\r\n      padding: 0px;\r\n      display: block;\r\n      text-decoration: none;\r\n          }\r\n\r\n    .related-post .owl-dots .owl-dot {\r\n          }\r\n\r\n      <\/style>\r\n      <script>\r\n      jQuery(document).ready(function($) {\r\n        $(\".related-post .post-list\").owlCarousel({\r\n          items: 3,\r\n          responsiveClass: true,\r\n          responsive: {\r\n            0: {\r\n              items: 1,\r\n            },\r\n            768: {\r\n              items: 2,\r\n            },\r\n            1200: {\r\n              items: 3,\r\n            }\r\n          },\r\n                      rewind: true,\r\n                                loop: true,\r\n                                center: true,\r\n                                autoplay: true,\r\n            autoplayHoverPause: true,\r\n                                nav: true,\r\n            navSpeed: 1000,\r\n            navText: ['<i class=\"fas fa-chevron-left\"><\/i>', '<i class=\"fas fa-chevron-right\"><\/i>'],\r\n                                dots: true,\r\n            dotsSpeed: 1200,\r\n                                                    rtl: false,\r\n          \r\n        });\r\n      });\r\n    <\/script>\r\n  <\/div>\r\n","protected":false},"excerpt":{"rendered":"<p>Quick Overview:The blog helps you discover how to use WebSockets in ASP.NET Core with step-by-step guidance. It provides [&hellip;]<\/p>\n  <div class=\"related-post slider\">\r\n        <div class=\"headline\">Related Posts<\/div>\r\n    <div class=\"post-list owl-carousel\">\r\n\r\n            <div class=\"item\">\r\n            <div class=\"thumb post_thumb\">\r\n    <a  title=\".Net Core vs. Java \u2013 Which One You Should Use for Your Projects?\" href=\"https:\/\/positiwise.com\/blog\/net-core-vs-java-technology\">\r\n\r\n      <img width=\"1200\" height=\"800\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2021\/12\/web-security-partner-1.jpg\" class=\"attachment-full size-full wp-post-image\" alt=\".Net Core vs. Java \u2013 Which One You Should Use for Your Projects?\" decoding=\"async\" loading=\"lazy\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2021\/12\/web-security-partner-1.jpg 1200w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2021\/12\/web-security-partner-1-300x200.jpg 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2021\/12\/web-security-partner-1-1024x683.jpg 1024w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2021\/12\/web-security-partner-1-768x512.jpg 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/>\r\n\r\n    <\/a>\r\n  <\/div>\r\n\r\n  <a class=\"title post_title\"  title=\".Net Core vs. Java \u2013 Which One You Should Use for Your Projects?\" href=\"https:\/\/positiwise.com\/blog\/net-core-vs-java-technology\">\r\n        .Net Core vs. Java \u2013 Which One You Should Use for Your Projects?  <\/a>\r\n\r\n        <\/div>\r\n              <div class=\"item\">\r\n            <div class=\"thumb post_thumb\">\r\n    <a  title=\"Precise Road Map for ASP.NET Developers\" href=\"https:\/\/positiwise.com\/blog\/roadmap-for-asp-dot-net-developers\">\r\n\r\n      <img width=\"1021\" height=\"621\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2022\/01\/dealership-1200x800-1.jpg\" class=\"attachment-full size-full wp-post-image\" alt=\"Hire ASP.NET Developers - A Precise Road Map to Employ ASP.NET Developers\" decoding=\"async\" loading=\"lazy\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2022\/01\/dealership-1200x800-1.jpg 1021w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2022\/01\/dealership-1200x800-1-300x182.jpg 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2022\/01\/dealership-1200x800-1-768x467.jpg 768w\" sizes=\"auto, (max-width: 1021px) 100vw, 1021px\" \/>\r\n\r\n    <\/a>\r\n  <\/div>\r\n\r\n  <a class=\"title post_title\"  title=\"Precise Road Map for ASP.NET Developers\" href=\"https:\/\/positiwise.com\/blog\/roadmap-for-asp-dot-net-developers\">\r\n        Precise Road Map for ASP.NET Developers  <\/a>\r\n\r\n        <\/div>\r\n              <div class=\"item\">\r\n            <div class=\"thumb post_thumb\">\r\n    <a  title=\"A Descriptive Guide to .NET Framework\" href=\"https:\/\/positiwise.com\/blog\/a-descriptive-guide-to-dot-net-framework\">\r\n\r\n      <img width=\"1021\" height=\"621\" src=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2022\/01\/a-descriptive-guide-to-dot-net-framework.jpg\" class=\"attachment-full size-full wp-post-image\" alt=\"A Descriptive Guide to .NET Framework\" decoding=\"async\" loading=\"lazy\" srcset=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2022\/01\/a-descriptive-guide-to-dot-net-framework.jpg 1021w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2022\/01\/a-descriptive-guide-to-dot-net-framework-300x182.jpg 300w, https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2022\/01\/a-descriptive-guide-to-dot-net-framework-768x467.jpg 768w\" sizes=\"auto, (max-width: 1021px) 100vw, 1021px\" \/>\r\n\r\n    <\/a>\r\n  <\/div>\r\n\r\n  <a class=\"title post_title\"  title=\"A Descriptive Guide to .NET Framework\" href=\"https:\/\/positiwise.com\/blog\/a-descriptive-guide-to-dot-net-framework\">\r\n        A Descriptive Guide to .NET Framework  <\/a>\r\n\r\n        <\/div>\r\n      \r\n  <\/div>\r\n\r\n  <script>\r\n      <\/script>\r\n  <style>\r\n    .related-post {}\r\n\r\n    .related-post .post-list {\r\n      text-align: left;\r\n          }\r\n\r\n    .related-post .post-list .item {\r\n      margin: 5px;\r\n      padding: 0px;\r\n          }\r\n\r\n    .related-post .headline {\r\n      font-size: 2rem !important;\r\n      color: #212529 !important;\r\n          }\r\n\r\n    .related-post .headline {\r\nmargin-top: 0;\r\npadding-left: 1%;\r\n}.related-post .post-list .item .post_thumb {\r\n      max-height: 220px;\r\n      margin: 0px 0px 10px 0px;\r\n      padding: 0px;\r\n      display: block;\r\n          }\r\n\r\n    .related-post .post-list .item .post_title {\r\n      font-size: 1rem;\r\n      color: #212529;\r\n      margin: 10px 0px;\r\n      padding: 0px;\r\n      display: block;\r\n      text-decoration: none;\r\n          }\r\n\r\n    .related-post .post-list .item .post_excerpt {\r\n      font-size: 13px;\r\n      color: #3f3f3f;\r\n      margin: 10px 0px;\r\n      padding: 0px;\r\n      display: block;\r\n      text-decoration: none;\r\n          }\r\n\r\n    .related-post .owl-dots .owl-dot {\r\n          }\r\n\r\n      <\/style>\r\n      <script>\r\n      jQuery(document).ready(function($) {\r\n        $(\".related-post .post-list\").owlCarousel({\r\n          items: 3,\r\n          responsiveClass: true,\r\n          responsive: {\r\n            0: {\r\n              items: 1,\r\n            },\r\n            768: {\r\n              items: 2,\r\n            },\r\n            1200: {\r\n              items: 3,\r\n            }\r\n          },\r\n                      rewind: true,\r\n                                loop: true,\r\n                                center: true,\r\n                                autoplay: true,\r\n            autoplayHoverPause: true,\r\n                                nav: true,\r\n            navSpeed: 1000,\r\n            navText: ['<i class=\"fas fa-chevron-left\"><\/i>', '<i class=\"fas fa-chevron-right\"><\/i>'],\r\n                                dots: true,\r\n            dotsSpeed: 1200,\r\n                                                    rtl: false,\r\n          \r\n        });\r\n      });\r\n    <\/script>\r\n  <\/div>\r\n","protected":false},"author":3,"featured_media":14216,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","footnotes":""},"categories":[7],"tags":[],"class_list":["post-14161","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dot-net-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>WebSockets in ASP.NET Core: How to Use Them<\/title>\n<meta name=\"description\" content=\"A guide to implementing WebSockets in ASP.NET Core for real-time bidirectional communication between client and server.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/positiwise.com\/blog\/websockets-in-asp-net-core\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"WebSockets in ASP.NET Core: How to Use Them\" \/>\n<meta property=\"og:description\" content=\"A guide to implementing WebSockets in ASP.NET Core for real-time bidirectional communication between client and server.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/positiwise.com\/blog\/websockets-in-asp-net-core\" \/>\n<meta property=\"og:site_name\" content=\"Positiwise\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/positiwise\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-23T08:54:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-27T08:45:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/how-to-use-websockets-in-asp-net-core.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1021\" \/>\n\t<meta property=\"og:image:height\" content=\"621\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Parag Mehta\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@positiwise\" \/>\n<meta name=\"twitter:site\" content=\"@positiwise\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Parag Mehta\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/websockets-in-asp-net-core#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/websockets-in-asp-net-core\"},\"author\":{\"name\":\"Parag Mehta\",\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/#\\\/schema\\\/person\\\/b4b3d9970ea94f5914a615f70345353a\"},\"headline\":\"WebSockets in ASP.NET Core: How to Use Them\",\"datePublished\":\"2023-10-23T08:54:09+00:00\",\"dateModified\":\"2023-12-27T08:45:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/websockets-in-asp-net-core\"},\"wordCount\":1803,\"publisher\":{\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/websockets-in-asp-net-core#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/how-to-use-websockets-in-asp-net-core.png\",\"articleSection\":[\".NET Development\"],\"inLanguage\":\"en-US\",\"copyrightYear\":\"2022\",\"copyrightHolder\":{\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/websockets-in-asp-net-core\",\"url\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/websockets-in-asp-net-core\",\"name\":\"WebSockets in ASP.NET Core: How to Use Them\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/websockets-in-asp-net-core#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/websockets-in-asp-net-core#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/how-to-use-websockets-in-asp-net-core.png\",\"datePublished\":\"2023-10-23T08:54:09+00:00\",\"dateModified\":\"2023-12-27T08:45:19+00:00\",\"description\":\"A guide to implementing WebSockets in ASP.NET Core for real-time bidirectional communication between client and server.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/websockets-in-asp-net-core#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/positiwise.com\\\/blog\\\/websockets-in-asp-net-core\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/websockets-in-asp-net-core#primaryimage\",\"url\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/how-to-use-websockets-in-asp-net-core.png\",\"contentUrl\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/how-to-use-websockets-in-asp-net-core.png\",\"width\":1021,\"height\":621,\"caption\":\"WebSockets in ASP.NET Core\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/websockets-in-asp-net-core#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/positiwise.com\\\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"WebSockets in ASP.NET Core: How to Use Them\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/\",\"name\":\"Positiwise\",\"description\":\"Providing Full-cycle Software Development Services\",\"publisher\":{\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/#organization\"},\"alternateName\":\"Positiwise Software Pvt Ltd\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/#organization\",\"name\":\"Positiwise\",\"alternateName\":\"Positiwise Software Pvt Ltd\",\"url\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/07\\\/logo.png\",\"contentUrl\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/07\\\/logo.png\",\"width\":202,\"height\":29,\"caption\":\"Positiwise\"},\"image\":{\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/positiwise\\\/\",\"https:\\\/\\\/x.com\\\/positiwise\",\"https:\\\/\\\/github.com\\\/positiwise\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/positiwise\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/#\\\/schema\\\/person\\\/b4b3d9970ea94f5914a615f70345353a\",\"name\":\"Parag Mehta\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/072ea4a638415a77610a840228d51b8225ad88856ed34d69cbe1fcd638390535?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/072ea4a638415a77610a840228d51b8225ad88856ed34d69cbe1fcd638390535?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/072ea4a638415a77610a840228d51b8225ad88856ed34d69cbe1fcd638390535?s=96&r=g\",\"caption\":\"Parag Mehta\"},\"description\":\"Parag Mehta, the CEO and Founder of Positiwise Software Pvt Ltd has extensive knowledge of the development niche. He is implementing custom strategies to craft highly-appealing and robust applications for its clients and supporting employees to grow and ace the tasks. He is a consistent learner and always provides the best-in-quality solutions, accelerating productivity.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/paragcmehta\"],\"url\":\"https:\\\/\\\/positiwise.com\\\/blog\\\/author\\\/parag-mehta\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"WebSockets in ASP.NET Core: How to Use Them","description":"A guide to implementing WebSockets in ASP.NET Core for real-time bidirectional communication between client and server.","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:\/\/positiwise.com\/blog\/websockets-in-asp-net-core","og_locale":"en_US","og_type":"article","og_title":"WebSockets in ASP.NET Core: How to Use Them","og_description":"A guide to implementing WebSockets in ASP.NET Core for real-time bidirectional communication between client and server.","og_url":"https:\/\/positiwise.com\/blog\/websockets-in-asp-net-core","og_site_name":"Positiwise","article_publisher":"https:\/\/www.facebook.com\/positiwise\/","article_published_time":"2023-10-23T08:54:09+00:00","article_modified_time":"2023-12-27T08:45:19+00:00","og_image":[{"width":1021,"height":621,"url":"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/how-to-use-websockets-in-asp-net-core.png","type":"image\/png"}],"author":"Parag Mehta","twitter_card":"summary_large_image","twitter_creator":"@positiwise","twitter_site":"@positiwise","twitter_misc":{"Written by":"Parag Mehta","Est. reading time":"15 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/positiwise.com\/blog\/websockets-in-asp-net-core#article","isPartOf":{"@id":"https:\/\/positiwise.com\/blog\/websockets-in-asp-net-core"},"author":{"name":"Parag Mehta","@id":"https:\/\/positiwise.com\/blog\/#\/schema\/person\/b4b3d9970ea94f5914a615f70345353a"},"headline":"WebSockets in ASP.NET Core: How to Use Them","datePublished":"2023-10-23T08:54:09+00:00","dateModified":"2023-12-27T08:45:19+00:00","mainEntityOfPage":{"@id":"https:\/\/positiwise.com\/blog\/websockets-in-asp-net-core"},"wordCount":1803,"publisher":{"@id":"https:\/\/positiwise.com\/blog\/#organization"},"image":{"@id":"https:\/\/positiwise.com\/blog\/websockets-in-asp-net-core#primaryimage"},"thumbnailUrl":"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/how-to-use-websockets-in-asp-net-core.png","articleSection":[".NET Development"],"inLanguage":"en-US","copyrightYear":"2022","copyrightHolder":{"@id":"https:\/\/positiwise.com\/blog\/#organization"}},{"@type":"WebPage","@id":"https:\/\/positiwise.com\/blog\/websockets-in-asp-net-core","url":"https:\/\/positiwise.com\/blog\/websockets-in-asp-net-core","name":"WebSockets in ASP.NET Core: How to Use Them","isPartOf":{"@id":"https:\/\/positiwise.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/positiwise.com\/blog\/websockets-in-asp-net-core#primaryimage"},"image":{"@id":"https:\/\/positiwise.com\/blog\/websockets-in-asp-net-core#primaryimage"},"thumbnailUrl":"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/how-to-use-websockets-in-asp-net-core.png","datePublished":"2023-10-23T08:54:09+00:00","dateModified":"2023-12-27T08:45:19+00:00","description":"A guide to implementing WebSockets in ASP.NET Core for real-time bidirectional communication between client and server.","breadcrumb":{"@id":"https:\/\/positiwise.com\/blog\/websockets-in-asp-net-core#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/positiwise.com\/blog\/websockets-in-asp-net-core"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/positiwise.com\/blog\/websockets-in-asp-net-core#primaryimage","url":"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/how-to-use-websockets-in-asp-net-core.png","contentUrl":"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/10\/how-to-use-websockets-in-asp-net-core.png","width":1021,"height":621,"caption":"WebSockets in ASP.NET Core"},{"@type":"BreadcrumbList","@id":"https:\/\/positiwise.com\/blog\/websockets-in-asp-net-core#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/positiwise.com\/blog"},{"@type":"ListItem","position":2,"name":"WebSockets in ASP.NET Core: How to Use Them"}]},{"@type":"WebSite","@id":"https:\/\/positiwise.com\/blog\/#website","url":"https:\/\/positiwise.com\/blog\/","name":"Positiwise","description":"Providing Full-cycle Software Development Services","publisher":{"@id":"https:\/\/positiwise.com\/blog\/#organization"},"alternateName":"Positiwise Software Pvt Ltd","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/positiwise.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/positiwise.com\/blog\/#organization","name":"Positiwise","alternateName":"Positiwise Software Pvt Ltd","url":"https:\/\/positiwise.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/positiwise.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/07\/logo.png","contentUrl":"https:\/\/positiwise.com\/blog\/wp-content\/uploads\/2023\/07\/logo.png","width":202,"height":29,"caption":"Positiwise"},"image":{"@id":"https:\/\/positiwise.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/positiwise\/","https:\/\/x.com\/positiwise","https:\/\/github.com\/positiwise","https:\/\/www.linkedin.com\/company\/positiwise"]},{"@type":"Person","@id":"https:\/\/positiwise.com\/blog\/#\/schema\/person\/b4b3d9970ea94f5914a615f70345353a","name":"Parag Mehta","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/072ea4a638415a77610a840228d51b8225ad88856ed34d69cbe1fcd638390535?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/072ea4a638415a77610a840228d51b8225ad88856ed34d69cbe1fcd638390535?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/072ea4a638415a77610a840228d51b8225ad88856ed34d69cbe1fcd638390535?s=96&r=g","caption":"Parag Mehta"},"description":"Parag Mehta, the CEO and Founder of Positiwise Software Pvt Ltd has extensive knowledge of the development niche. He is implementing custom strategies to craft highly-appealing and robust applications for its clients and supporting employees to grow and ace the tasks. He is a consistent learner and always provides the best-in-quality solutions, accelerating productivity.","sameAs":["https:\/\/www.linkedin.com\/in\/paragcmehta"],"url":"https:\/\/positiwise.com\/blog\/author\/parag-mehta"}]}},"_links":{"self":[{"href":"https:\/\/positiwise.com\/blog\/wp-json\/wp\/v2\/posts\/14161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/positiwise.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/positiwise.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/positiwise.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/positiwise.com\/blog\/wp-json\/wp\/v2\/comments?post=14161"}],"version-history":[{"count":20,"href":"https:\/\/positiwise.com\/blog\/wp-json\/wp\/v2\/posts\/14161\/revisions"}],"predecessor-version":[{"id":14648,"href":"https:\/\/positiwise.com\/blog\/wp-json\/wp\/v2\/posts\/14161\/revisions\/14648"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/positiwise.com\/blog\/wp-json\/wp\/v2\/media\/14216"}],"wp:attachment":[{"href":"https:\/\/positiwise.com\/blog\/wp-json\/wp\/v2\/media?parent=14161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/positiwise.com\/blog\/wp-json\/wp\/v2\/categories?post=14161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/positiwise.com\/blog\/wp-json\/wp\/v2\/tags?post=14161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}