Skip to content
Tomas Fabian edited this page Aug 24, 2013 · 1 revision

Welcome to the PushPro wiki!

How to create a PushPro server with SignalR self hosting

(1.) run the http listener:

using System;
using Microsoft.Owin.Hosting;

namespace Example.PushPro.Server.SignalR
{
    class Program
    {
        static void Main(string[] args)
        {
            using (WebApp.Start<Startup>("http://localhost:49895/api/"))
            {
                Console.WriteLine("Server running at http://localhost:49895/api");
                Console.ReadLine();
            }
        }
    }
}

(2.) map your hubs:

using Microsoft.AspNet.SignalR;
using Owin;

namespace Example.PushPro.Server.SignalR
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapHubs("Books", new HubConfiguration());
        } 
    }
}

(3.) Create your PushProHub that will publish your reactive events to the subscribed clients. PushProHub internally applies the provided OData query from each client with the help of the ASP.NET Web API OData, so the notifications are filtered and paged on the server side.

using System;
using System.Linq;
using System.Reactive.Linq;
using Example.PushPro.Client.SignalR.FakeReposoitory;
using Example.PushPro.Server.SignalR.Domain;
using PushPro.Server;

namespace Example.PushPro.Server.SignalR.Hubs
{
    public class BooksHub : PushProHub<Book>
    {
        private readonly Repository repository;

        public BooksHub()
        {
            this.repository = new Repository();
        }
        
        protected override IQbservable<Book> Source
        {
            get
            {
                int booksCount = this.repository.Books.Count();

                return Observable.Interval(TimeSpan.FromSeconds(1))
                                 .Select(i => repository.Books.FirstOrDefault(b => b.Id == i))
                                 .Where(c => c != null)
                                 .Take(booksCount)
                                 .AsQbservable();
            }
        }
    }
}

How to create a PushPro client

(1.) Create a SignalRPushProvider with the correct uri and hub name. Then inject it into an instance of PushService which exposes the Where, Skip and Top operators. After you call the ToObservable extension method, which creates a connection with the server, you can add additional Rx operators for client side operations and subscribe to the observable sequence.

using System;
using Example.PushPro.Server.SignalR.Domain;
using PushPro.Client;
using PushPro.Client.SignalR;

namespace Example.PushPro.Client.SignalR
{
    class Program
    {
        static void Main(string[] args)
        {
            var booksSubscription = SubscribeToBooks();

            Console.ReadKey();

            using (booksSubscription)
            {
            }
        }

        private static IDisposable SubscribeToBooks()
        {
            var pushProvider = new SignalRPushProvider<Book>(new Uri(@"http://localhost:49895/api/Books"), "BooksHub");

            return new PushService<Book>(pushProvider)
                .Where(c => c.Id != 5)
                .Skip(1)
                .Take(7)
                .ToObservable()
                .Subscribe(
                    value => Console.WriteLine("Id: " + value.Id + " Book Title: '" + value.Title + "'"),
                    error => Console.WriteLine("Books error: " + error.Message),
                    () => Console.WriteLine("Books Completed"));
        }
    }
}

Results:

Id: 2 Book Title: ‘Title 2’
Id: 3 Book Title: ‘Title 3’
Id: 4 Book Title: ‘Title 4’
Id: 6 Book Title: ‘Title 6’
Id: 7 Book Title: ‘Title 7’
Id: 8 Book Title: ‘Title 8’
Id: 9 Book Title: ‘Title 9’
Books Completed

The downloadable source code contains a Example SignalR client and server implementation.

Clone this wiki locally