Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

periodically sending messages to all clients using signalr

I want to send some data from server to all connected clients using hubs after a specific interval. How can I accomplish this using signalr hubs.

like image 908
Ibrahim Avatar asked Oct 24 '25 17:10

Ibrahim


2 Answers

Spin up the System.Threading.Timer, and from it's callback broadcast the message using specific hub.

Global.asax:

private Timer timer;
public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.MapHubs("~/signalr2");
        timer = new Timer(TimerCallback(timerCallback), null, Timeout.Infinite, 1000);
    }
}

Check the “Broadcasting over a Hub from outside of a Hub” section in SignalR wiki page.

like image 85
Artem Koshelev Avatar answered Oct 26 '25 07:10

Artem Koshelev


Use ReactiveExtensions and then setup an Observable.Interval call. Then reactive will automatically call the lambda which can broadcast to your clients.

like image 44
leon.io Avatar answered Oct 26 '25 09:10

leon.io



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!