Aug 21 2023
Install-Package Microsoft.AspNetCore.SignalR.Client
public class NotificationsHub : Hub { public async Task SendNotification(string message) { await Clients.All.SendAsync("ReceiveNotificaiton", message); } }
var builder = WebApplication.CreateBuilder(args); builder.Services.AddSignalR(); var app = builder.Build(); app.MapHub<NotificationsHub>("notifications-hub"); app.Run();
<input type="text" id="messageBox" placeholder="Your message" /> <button id="sendButton">Send</button> <ul id="messageList"></ul>
<script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/6.0.1/signalr.js"></script>
<script> $(function() { const connection = new signalR.HubConnectionBuilder() .withUrl("/notifications-hub") .configureLogging(signalR.LogLevel.Information) .build(); async function start() { try { await connection.start(); console.log("SignalR Connected."); await connection.invoke("SendNotification", "Initial Notification"); } catch (err) { console.log(err); setTimeout(start, 5000); } }; connection.onclose(async () => { await start(); }); start(); }); </script>
$("#sendButton").on('click', function() { var message = $("#messageBox").val(); connection.invoke("SendNotification", message); });
connection.on("ReceiveNotification", (message) => { const li = document.createElement("li"); li.textContent = `${message}`; document.getElementById("messageList").appendChild(li); });
1. Design Patterns that Deliver
This isn’t just another design patterns book. Dive into real-world examples and practical solutions to real problems in real applications.Check out it here.
Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.
Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.
Join 18,000+ subscribers to improve your .NET Knowledge.
Subscribe to the TheCodeMan.net and be among the 18,000+ subscribers gaining practical tips and resources to enhance your .NET expertise.