I´m using SignalR/PersistentConnection, not the Hub.
I want to send a message from the server to client. I have the client id to send it, but how can I send a message from server to the client?
Like, when some event happens on server, we want send a notification to a particular user.
Any ideas?
The github page shows how to do this using PersistentConnections.
public class MyConnection : PersistentConnection {
protected override Task OnReceivedAsync(string clientId, string data) {
// Broadcast data to all clients
return Connection.Broadcast(data);
}
}
Global.asax
using System;
using System.Web.Routing;
using SignalR.Routing;
public class Global : System.Web.HttpApplication {
protected void Application_Start(object sender, EventArgs e) {
// Register the route for chat
RouteTable.Routes.MapConnection<MyConnection>("echo", "echo/{*operation}");
}
}
Then on the client:
$(function () {
var connection = $.connection('echo');
connection.received(function (data) {
$('#messages').append('<li>' + data + '</li>');
});
connection.start();
$("#broadcast").click(function () {
connection.send($('#msg').val());
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With