I'm having troubles to update only one specific user.
I'm working on some asp.net core web application and I want to implement push notification to my project. I'll explain a little more about what am i doing in my application.
I have a page where users can post something (like on facebook). On that post everyone can like/unlike it (like button) or comment it (can delete comments too). And my implementation of signalR on that part is working perfectly with await Clients.All.SendAsync("UpdateComments", userId, postId);
.
Now i want to send notification to the user whose post is.
I tries to do that on this way await Clients.User(userId).SendAsync("SendNotification", notificationText);
but not working. When i debuged it, on this part User(someId)
it expect me to send connectionId
which i don't have for user which i want to update. I have connectionId only for user who invoked current connection.
My question is: How can i update user with userId from my table or how can i get connectionId based on my userId? OR If i can't on any way to accomplish this, is there a way that i can automatically add user (when he log in to application) in some signalR group connection whithout need for user to activate connection by himself?
I googled a lot, but i didn't found answer. This is link which i followed to implement signalR in my app.
I'm already write small blog post here
You need to write a method to send to specific user id like this
public async Task Send(string userId)
{
var message = $"Send message to you with user id {userId}";
await Clients.Client(userId).SendAsync("ReceiveMessage", message);
}
Then listen on event
(function () {
var connection = new signalR.HubConnectionBuilder().withUrl("/connectionHub").build();
connection.start().then(function () {
console.log("connected");
connection.invoke('getConnectionId')
.then(function (connectionId) {
sessionStorage.setItem('conectionId', connectionId);
// Send the connectionId to controller
}).catch(err => console.error(err.toString()));;
});
$("#sendmessage").click(function () {
var connectionId = sessionStorage.getItem('conectionId');
connection.invoke("Send", connectionId);
});
connection.on("ReceiveMessage", function (message) {
console.log(message);
});
})();