4

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.

Ibrahim
  • 267
  • 1
  • 6
  • 18
  • 1
    Have you looked at the SignalR.Sample NuGet package? It does this. We plan to publish a tutorial on the ASP.NET site about it in the next week or two. – tdykstra Mar 19 '13 at 16:16

4 Answers4

6

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.

Community
  • 1
  • 1
Artem Koshelev
  • 10,548
  • 4
  • 36
  • 68
  • I tried it this way too. But its still not working. For double checking i added var context = GlobalHost.ConnectionManager.GetHubContext(); context.Clients.All.PublishMessage(nick, message); in sendmessage method of simple chat application. But it doesn't work. – Ibrahim Mar 19 '13 at 11:56
  • context.Clients.All.PublishMessage removing the All from this worked for me. – Ibrahim Mar 19 '13 at 12:48
  • 1
    @Ibrahim i removing the All worked for you then you need to update SignalR. You're using a really old version and we're released about 6 more since then. – davidfowl Mar 19 '13 at 17:55
1

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

leon.io
  • 2,779
  • 1
  • 18
  • 26
1

I have stumbled upon this post by Jason Roberts => http://dontcodetired.com/blog/post/Using-Server-Side-Timers-and-SignalR-in-ASPNET-MVC-Applications.aspx

He uses IRegisteredObject and HostingEnvironment.RegisterObject then a System.Threading.Timer in the class that does the work, I haven't tried it myself, but it looks exactly the sort of thing.

Luke T O'Brien
  • 2,565
  • 3
  • 26
  • 38
-3

Just add

Thread.Sleep(5000);

in your send Method.

Ex:

    public void Send(string name, string message)
    {
        Thread.Sleep(5000);
        //call the broadcast message to upadate the clients.
        Clients.All.broadcastMessage(name, message); 
    }

Hope it helps.

Edit

The following code renders the current time for every 5 seconds.

Here is script for it:

<script type="text/javascript">
    $(function () {
        $.connection.hub.logging = true;
        $.connection.hub.start();
        // Declare a proxy to reference the hub.  
        var chat = $.connection.chatHub;

        //Appending the responce from the server to the discussion id
        chat.client.currentTime = function (time) {
            $('#discussion').append("<br/>" + time + "<br/>");
        };

        // Start the connection. 
        $.connection.hub.start().done(function () {

            //Call the server side method for every 5 seconds
            setInterval(function () {
                var date = new Date();
                chat.client.currentTime(date.toString());
            }, 5000);
        });

    }); 
</script>

<div id="discussion"></div>

And on the HubClass write the following:

public class ChatHub: Hub
   {
        public void currentTime(string date)
        {
            Clients.All.broadCastTime(date);
        }
   }
Karthik Chintala
  • 5,465
  • 5
  • 30
  • 60
  • I don't want to delay the sending of message. Rather i want to send them some message periodically. Say after each 5 seconds i want to send them current date time. – Ibrahim Mar 19 '13 at 10:45
  • @Ibrahim Use timer. Either from c# or jquery ajax function using setinterval. – Monie corleone Mar 19 '13 at 10:48
  • 2
    how would i use the timer from server side (c#). just the small example. I know how to use from client side. – Ibrahim Mar 19 '13 at 10:55
  • @Ibrahim you should use setInterval for that purpose. I'll edit my post in couple of minutes to give you the code for running periodically – Karthik Chintala Mar 19 '13 at 11:04
  • 11
    I fail to see how this answers the original question - OP wants to broadcast updates from the server to connected clients on a server side timer (not pushed from other clients or simply delayed with a thread.sleep). – Matt Randle Feb 15 '15 at 13:18