vidriduch
vidriduch

Reputation: 4863

SignalR.EventAggregatorProxy simple example issues

I'm trying to use SignalR.EventAggregatorProxy library, but I cannot get my head arround the docs or the demos. I do not need to use Ninject and I'm only after very simple tes app with WEB API on server side and Angular SPA.

Here is what I Have so far:

Startup.cs

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.MapSignalR();

        var proxy = new Lazy<IEventAggregator>(() => new Eventer());
        GlobalHost.DependencyResolver.Register(typeof(IEventAggregator), () => proxy.Value);

        app.MapEventProxy<Message>();
    }
}

message classes

public abstract class Message {}

public class MyMessage : Message
{
    public int Number { get; set; }
}

Eventer.cs class firing events

public class Eventer : IEventAggregator
{
    public void Subscribe(Action<object> handler)
    {
        Task.Factory.StartNew(() =>
        {
            int number = 0;

            while (true)
            {
                handler.Invoke(new MyMessage { Number = number++ });
                System.Threading.Thread.Sleep(1000);
            }
        });
    }
}

simple app.js

(function () {
    'use strict';

    var app = angular.module("TestApp", ['signalR.eventAggregator']);

    app.controller("TestCtrl", ['$scope','$http', function ($scope, $http) {

        function onEvent(e) {
            console.log("event received => ", e);
        };

        $scope.eventAggregator().subscribe(EventAgrTest.Events.MyMessage, onEvent);

        $scope.headerText = "HEADER";
    }]);
})();

and index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="TestApp">
<head>
    <title></title>

    <script src="Scripts/jquery-1.6.4.js"></script>
    <script src="Scripts/jquery.signalR-2.2.0.js"></script>
    <script src="Scripts/jquery.signalR.eventAggregator-1.4.141.0.js"></script>
    <script src="/signalr/hubs"></script>
    <script src="/eventAggregation/events"></script>
</head>
<body ng-controller="TestCtrl">

    <h1>{{headerText}}</h1>

    <script src="Scripts/angular.js"></script>
    <script src="Scripts/jquery.signalR.eventAggregator.angular-1.4.143.0.js"></script>

    <script src="Scripts/app.js"></script>
</body>
</html>

When I run the app I'm getting "TypeError: Cannot read property 'subscribe' of undefined" error pointing to line

signalR.eventAggregator.subscribe(type, function(e) { ...

in jquery.signalR.eventAggregator.angular-1.4.143.0.js

I'm sure I'm missing something, but I'm not sure what.

There is part in the Wiki called Implement constraint handlers (https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/wiki/Implement-constraint-handlers) per which it should be as simple as inheriting from abstract class, but I'm not sure what needs to inherit from this class.

console output

Upvotes: 2

Views: 545

Answers (2)

vidriduch
vidriduch

Reputation: 4863

The problem was in the order of JS dependencies in index.html file.

<script src="/signalr/hubs"></script>

should be before

<script src="Scripts/jquery.signalR.eventAggregator-1.4.141.0.js"></script>

Here is how it should look like ...

<script src="Scripts/jquery-1.6.4.js"></script>
<script src="Scripts/jquery.signalR-2.2.0.js"></script>
<script src="/signalr/hubs"></script>
<script src="Scripts/jquery.signalR.eventAggregator-1.4.141.0.js"></script>
<script src="/eventAggregation/events"></script>

<script src="Scripts/angular.js"></script>
<script src="Scripts/jquery.signalR.eventAggregator.angular-1.4.143.0.js"></script>

it does make a sense ... now ...

Upvotes: 1

Anders
Anders

Reputation: 17564

im the author of the library,

It was not designed to use simple data types as messages.

You need a message base class for example

public abstract class Message {}

Create a actual message

public class MyMessage : Message 
{
    public int Number { get;set; }
}

Now in the config change use the base class, this is a way to let the library know which messagss should be proxied to javascript

app.MapEventProxy<Message>();

In your event proxy change to

handler.Invoke(new MyMessage { Number = number++ });

Now in your client change to

$scope.eventAggregator().subscribe(EventAgrTest.Events.MyMessage, onEvent);

I should update the Invoke method from taking type object to take a generic type with constraint class

Upvotes: 1

Related Questions