Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET C# SignalR Stream to Client

I've got a couple SignalR basic demos working locally (chat, etc.) and I understand pushing to the server then having it broadcast to all connected clients.

My question is how might I utilize SignalR to "stream" data to the client. For example, seen examples in NodeJS/Socket.IO utilizing Twitters streaming API.

Are there any SignalR streaming examples out there? How might this work?

like image 819
aherrick Avatar asked Nov 16 '11 14:11

aherrick


2 Answers

Take a look at the samples in the SignalR.Samples project. There's a streaming sample in there.

Server side connection:

namespace SignalR.Samples.Streaming
{
    public class Streaming : PersistentConnection
    {
    }
}

Code that generates the stream

ThreadPool.QueueUserWorkItem(_ =>
{
    var connection = Connection.GetConnection<Streaming.Streaming>();
    while (true)
    {
        connection.Broadcast(DateTime.Now.ToString());
        Thread.Sleep(2000);
    }
});

Client Side Code

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <script src="../Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
    <script src="../Scripts/jquery.signalR.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            var connection = $.connection('Streaming.ashx');

            connection.received(function (data) {
                $('#messages').append('<li>' + data + '</li>');
            });

            connection.start();
        });
    </script>
</head>
<body>
    <ul id="messages">
    </ul>
</body>
</html>
like image 184
davidfowl Avatar answered Sep 29 '22 08:09

davidfowl


Why not just use SignalR to tell the client to start a download over HTTP the old fashioned way? Or are you using Stream in a different way in terms of .NET?

If you just mean broadcasting external events, there is nothing that says that one of the clients can't be some other service that is sending in interesting data events to relay.

like image 26
Paul Tyng Avatar answered Sep 29 '22 07:09

Paul Tyng