We are no longer accepting contributions to Documentation. Please see our post on meta.

AngularJS

SignalR with AngularJs 0.9.1

0.9.0
0.9.1
0.9.2
0.9.3
0.9.4
0.9.5
0.9.6
0.9.7
0.9.9
0.9.10
0.9.11
0.9.12
0.9.13
0.9.14
0.9.15
0.9.16
0.9.17
0.9.18
0.9.19
0.10.0
0.10.1
0.10.2
0.10.3
0.10.4
0.10.5
0.10.6
1.0.0rc1
g3-v1.0.0rc1
g3-v1.0.0-rc2
v1.0.0rc2
v1.0.0rc3
v1.0.0rc4
v1.0.0rc5
v1.0.0rc6
v1.0.0rc7
v1.0.0rc8
v1.0.0rc9
v1.0.0rc10
v1.0.0rc11
v1.0.0rc12
1.0.0
1.0.1
1.0.2
1.1.0
1.0.3
1.1.1
1.0.4
1.1.2
1.0.5
1.1.3
1.0.6
1.1.4
1.0.7
1.1.5
1.2.0rc1
1.0.8
1.2.0-rc.2
1.2.0-rc.3
1.2.0
1.2.1
1.2.2
1.2.3
1.2.4
1.2.5
1.2.6
1.2.7
1.2.8
1.2.9
1.2.10
1.2.11
1.2.12
1.2.13
1.2.14
1.3.0-beta.1
1.3.0-beta.2
1.3.0-beta.3
1.2.15
1.3.0-beta.4
1.2.16
1.3.0-beta.5
1.3.0-beta.6
1.3.0-beta.7
1.3.0-beta.8
1.3.0-beta.9
1.3.0-beta.10
1.2.17
1.3.0-beta.11
1.2.18
1.3.0-beta.12
1.3.0-beta.13
1.2.19
1.3.0-beta.14
1.2.20
1.3.0-beta.15
1.3.0-beta.16
1.2.21
1.3.0-beta.17
1.2.22
1.3.0-beta.18
1.2.23
1.3.0-beta.19
1.3.0-rc.0
1.2.24
1.3.0-rc.1
1.2.25
1.3.0-rc.2
1.3.0-rc.3
1.3.0-rc.4
1.2.26
1.3.0-rc.5
1.3.0
1.3.1
1.3.2
1.3.3
1.2.27
1.3.4
1.3.5
1.3.6
1.3.7
1.2.28
1.3.8
1.4.0-beta.0
1.3.9
1.3.10
1.4.0-beta.1
1.3.11
1.4.0-beta.2
1.3.12
1.4.0-beta.3
1.3.13
1.4.0-beta.4
1.3.14
1.4.0-beta.5
1.3.15
1.4.0-beta.6
1.4.0-rc.0
1.4.0-rc.1
1.4.0-rc.2
1.4.0
1.3.16
1.4.1
1.3.17
1.4.2
1.4.3
1.4.4
1.3.18
1.4.5
1.3.19
1.4.6
1.5.0-beta.0
1.2.29
1.3.20
1.4.7
1.5.0-beta.1
1.5.0-beta.2
1.4.8
1.5.0-rc.0
1.5.0-rc.1
1.4.9
1.5.0-rc.2
1.5.0
1.4.10
1.5.1
1.5.2
1.5.3
1.5.4
1.5.5
1.4.11
1.5.6
1.4.12
1.5.7
1.2.30
1.5.8
1.2.31
1.4.13
1.2.32
1.6.0-rc.0
1.6.0-rc.1
1.5.9
1.6.0-rc.2
1.6.0
1.5.10
1.6.1
1.5.11
1.6.2
1.6.3
1.6.4
1.6.5

In this Article we focus on "How to create a simple project using AngularJs And SignalR", in this training you need to know about "how create app with angularjs", "how to create/use service on angular" And basic knowledge about SignalR" for this we recommend https://www.codeproject.com/Tips/590660/Introduction-to-SignalR).

This draft deletes the entire topic.

Examples

  • 1

    step 1: Create Project

    - Application
        - app.js
        - Controllers
            - appController.js
        - Factories
            - SignalR-factory.js
    - index.html
    - Scripts
        - angular.js
        - jquery.js
        - jquery.signalR.min.js
    - Hubs
    

    SignalR version use: signalR-2.2.1

    Step 2: Startup.cs And ChatHub.cs

    Go to your "/Hubs" directory and Add 2 files [Startup.cs, ChatHub.cs]

    Startup.cs

    using Microsoft.Owin;
    using Owin;
    [assembly: OwinStartup(typeof(SignalR.Hubs.Startup))]
    
    namespace SignalR.Hubs
    {
        public class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                app.MapSignalR();
            }
        }
    }
    

    ChatHub.cs

    using Microsoft.AspNet.SignalR;
    
    namespace SignalR.Hubs
    {
        public class ChatHub : Hub
        {
            public void Send(string name, string message, string time)
            {
                Clients.All.broadcastMessage(name, message, time);
            }
        }
    }
    

    step 3: create angular app

    Go to your "/Application" directory and Add [app.js] file

    app.js

    var app = angular.module("app", []);
    

    step 4: create SignalR Factory

    Go to your "/Application/Factories" directory and Add [SignalR-factory.js] file

    SignalR-factory.js

    app.factory("signalR", function () {
        var factory = {};
    
        factory.url = function (url) {
            $.connection.hub.url = url;
        }
    
        factory.setHubName = function (hubName) {
            factory.hub = hubName;
        }
    
        factory.connectToHub = function () {
            return $.connection[factory.hub];
        }
    
        factory.client = function () {
            var hub = factory.connectToHub();
            return hub.client;
        }
    
        factory.server = function () {
            var hub = factory.connectToHub();
            return hub.server;
        }
    
        factory.start = function (fn) {
            return $.connection.hub.start().done(fn);
        }
    
        return factory;
    });
    

    step 5: update app.js

    var app = angular.module("app", []);
    
    app.run(function(signalR) {
        signalR.url("http://localhost:21991/signalr");
    });
    

    localhost:21991/signalr | this is your SignalR Hubs Urls

    step 6: add controller

    Go to your "/Application/Controllers" directory and Add [appController.js] file

    app.controller("ctrl", function ($scope, signalR) {
        $scope.messages = [];
        $scope.user = {};
    
        signalR.setHubName("chatHub");
    
        signalR.client().broadcastMessage = function (name, message, time) {
            var newChat = { name: name, message: message, time: time };
    
            $scope.$apply(function() {
                $scope.messages.push(newChat);
            });
        };
    
        signalR.start(function () {
            $scope.send = function () {
                var dt = new Date();
                var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
    
                signalR.server().send($scope.user.name, $scope.user.message, time);
            }
        });
    });
    

    signalR.setHubName("chatHub") | [ChatHub] (public class) > ChatHub.cs

    Note: do not insert HubName with upper Case, first letter is lower Case.

    signalR.client() | this method try to connect to your hubs and get all functions in the Hubs, in this sample we have "chatHub", to get "broadcastMessage()" function;

    step 7: add index.html in route of directory

    index.html

    <!DOCTYPE html>
    <html ng-app="app" ng-controller="ctrl">
    <head>
        <meta charset="utf-8" />
        <title>SignalR Simple Chat</title>
    </head>
    <body>
        <form>
            <input type="text" placeholder="name" ng-model="user.name" />
            <input type="text" placeholder="message" ng-model="user.message" />
            <button ng-click="send()">send</button>
    
            <ul>
                <li ng-repeat="item in messages">
                    <b ng-bind="item.name"></b> <small ng-bind="item.time"></small> : {{item.message}}
                </li>
            </ul>
        </form>
    
        <script src="Scripts/angular.min.js"></script>
        <script src="Scripts/jquery-1.6.4.min.js"></script>
        <script src="Scripts/jquery.signalR-2.2.1.min.js"></script>
        <script src="signalr/hubs"></script>
        <script src="app.js"></script>
        <script src="SignalR-factory.js"></script>
    </body>
    </html
    

    Result with Image

    User 1 (send and receive)

    User 2 (send and receive)

Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Remarks

Remarks

Still have a question about SignalR with AngularJs? Ask Question

Topic Outline


    We are no longer accepting contributions to Documentation. Drafts cannot be modified.