Hi @osyris , I followed these 2 documents to create a sample to show you my idea on this case, hope it helps.
My thought is when the user start a chat, you can call a service like JoinGroup
in my sample. Here, you need to set a unique group name, then save it in some where.
Next you need to made the manager page to update the notification, and when the manager click the new notification, he will be added to the group(call the JoinGroup
method again with the same groupname), then these 2 people can start a conversation.
ChatHub.cs
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;
namespace WebApplication1.Hubs
{
public class ChatHub : Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
public Task JoinGroup(string group)
{
//user call this method with a group name as the parameter
//if(user call this method){
// do some action to save group name
// return Groups.AddToGroupAsync(Context.ConnectionId, group);
//}else if(manager call this method){
// get group name
// return Groups.AddToGroupAsync(Context.ConnectionId, group);
//}
return Groups.AddToGroupAsync(Context.ConnectionId, group);
}
public Task SendMessageToGroup(string groupname, string sender, string message)
{
return Clients.Group(groupname).SendAsync("ReceiveMessage", sender, message);
}
}
}
chat.js
"use strict";
var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();
//Disable send button until connection is established
document.getElementById("sendButton").disabled = true;
connection.on("ReceiveMessage", function (user, message) {
var li = document.createElement("li");
document.getElementById("messagesList").appendChild(li);
li.textContent = `${user} says ${message}`;
});
connection.start().then(function () {
document.getElementById("sendButton").disabled = false;
}).catch(function (err) {
return console.error(err.toString());
});
//document.getElementById("sendButton").addEventListener("click", function (event) {
// var user = document.getElementById("userInput").value;
// var message = document.getElementById("messageInput").value;
// connection.invoke("SendMessage", user, message).catch(function (err) {
// return console.error(err.toString());
// });
// event.preventDefault();
//});
document.getElementById("sendButton").addEventListener("click", function (event) {
var user = document.getElementById("userInput").value;
var message = document.getElementById("messageInput").value;
connection.invoke("SendMessageToGroup","PrivateGroup", user, message).catch(function (err) {
return console.error(err.toString());
});
event.preventDefault();
});
document.getElementById("btnPrivateGroup").addEventListener("click", function (event) {
var groupName = "PrivateGroup";
connection.invoke("JoinGroup", groupName).catch(function (err) {
return console.error(err.toString());
//if user call joingroup successfully, then write code to update the manager page's notification
});
event.preventDefault();
});
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
TinyWang