how to integrate asp.net mvc page containing signalR with other platforms

The question is not a duplicate of this, although the mentioned question has some very interesting answers and helped me to understand my problem better but it doesn’t fulfill my need. Let me explain my scenario.

I have an asp.net mvc website, having notification functionality and real time data updates by signalR & SQL dependency. User authentication is being done by using Identity 2.0.
Only authorized users are allowed to see the updated data/notifications. Furthermore the notification/updates vary user to user. I’ve achieved this by implementing custom UserId provider and using Identity’s UserId.

Read More

Now I want to achieve following goals.

  • Easy part: Display the content of the page (dashboard) in other websites regardless of their development language. It can be injected inside existing page or wherever they (other websites) want.

  • Hard part: Show the real time notifications & updates on the integrated portion based on logged in user.

What would be the best course of actions to perform in this scenario?

Update
Since the original question was put on hold due to not specifying the details, so here are the details on which I would like to get suggestions.

I’ve a asp.net mvc website running at localhost:54603, Following is the action of the home controller

public ActionResult Index()
{
   HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "*");
   return View();               
}

Index view has some signalR functionality on it. Below is the startup configuration of signalR to allow CORS since I my goal is to expose the index page and its whole functionality to clients (running php etc)

app.Map("/signalr", map =>
{

  map.UseCors(CorsOptions.AllowAll);
  var hubConfiguration = new HubConfiguration
  {
       // You can enable JSONP by uncommenting line below.
       // JSONP requests are insecure but some older browsers (and some
       // versions of IE) require JSONP to work cross domain
       // EnableJSONP = true
   };

  map.RunSignalR(hubConfiguration);
});

I’ve created notifcationHub and following is my code at front end (using proxy).

<script>
    $(function () {
        // Reference the auto-generated proxy for the hub.
        $.connection.notificationHub.url = 'http://localhost:54603/signalr';
        var notification = $.connection.notificationHub;

        // Client side method for receiving the list of notifications on the connected event from the server
        notification.client.refreshNotification = function (data) {
            $("#notificationTab").empty();
            $("#cntNotifications").text(data.length);
            for (var i = 0; i < data.length; i++) {
                $("#notificationTab").append("<tr> <td> " + data[i].Id + "</td> <td>" + data[i].Text + "</td> <td>" + data[i].CreatedDate + "</td></tr>");
            }
        }

        //Client side method which will be invoked from the Global.asax.cs file.
        notification.client.addLatestNotification = function (data) {       
            $("#cntNotifications").text($("#cntNotifications").text() + 1);
            $("#notificationTab").append("<tr> <td> " + data.Id + "</td> <td>" + data.Text + "</td> <td>" + data.CreatedDate + "</td></tr>");
        }

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

            //When the send button is clicked get the text and user name and send it to server.
            $("#btnSend").click(function () {
                notification.server.sendNotification($("#text").val(), $("#userName").val());
            });
            console.log($.connection.hub.id);

        }).fail(function (data) { console.log('Could not connect' + data); });

    });
</script>

and Html is following

@{
    ViewBag.Title = "Home Page";
    Layout = null;
}

<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/signalr/jquery.signalr-2.2.0.min.js"></script>

<script src="@Url.Content("~/signalr/hubs")"></script>

@*<script src="~/signalr/hubs"></script>*@

<div style="width: 70%; padding: 20px">
    <div class="panel panel-primary">
        <div class="panel-heading">

            <! &ndash; To show notification count-->
            <div style="float: left" class="panel-title">Notifications</div>
            <div style="float: right" class="badge" id="cntNotifications"></div>
            <div style="clear: both"></div>


        </div>
        <div class="panel-body">
            <! &ndash; To show All the notifications-->
            <table class="table table-striped table-hover ">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Text</th>
                        <th>Created Date</th>
                    </tr>
                </thead>

                <tbody id="notificationTab"></tbody>
            </table>
        </div>
    </div>

    <! &ndash; Add panel notification to send notification, Make sure that user enters the user id of the domain they are logged into -->
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">Create Notifications</h3>
        </div>
        <div class="panel-body">
            <div class="form-group">
                <label class="control-label" for="focusedInput">Notification Text</label>
                <input class="form-control" id="text" type="text" value="">
            </div>
            <div class="form-group">
                <label class="control-label" for="focusedInput">Send To</label>
                <input class="form-control" id="userName" type="text" value="">
            </div>
            <a id="btnSend" style="cursor: pointer" class="btn btn-primary">Send Notification</a>
        </div>
    </div>
</div>

The page runs fine as expected and connection id is being logged in console.
enter image description here

Now I’ve created another html page and following is its code

<html>
<head>
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>  
    <script src="http://ajax.aspnetcdn.com/ajax/signalr/jquery.signalr-2.2.0.min.js"></script> 
</head>
<body>
    <aside>
         <div class="col-lg-2">
              <ul>
                  <li>this</li>
                  <li>is</li>
                  <li>aside</li>
              </ul>
              </div>
    </aside>
    <article>
        <div class="col-lg-10">
            <div id="something">

            </div>
        </div>
    </article>
    <script src="http://localhost:54603/signalr/hubs"></script>
    <script>
        $(document).ready(function () {
            $.ajax({
                url: "http://localhost:54603/Home/Index",
                dataType: 'html',
                crossDomain: true,
                cache:true


            }).done(function (data) {
                $('#something').html(data);
            });
        });
    </script>
</body>
</html>

Ajax request is returning the html but the signalR throws error. following are the console messages.

GET http://localhost:54603/Home/Index 200 OK 1.18s
Could not connect Error: Error during negotiation request.

What am I doing wrong which is causing this error?

Related posts

1 comment

  1. It’s difficult to pin down exactly what you need to do, cause the question is rather broad, but I hope that these points help(ed) somewhat:

    SignalR cross-domain uses JavaScript on the client side and CORS on the server side, so you’re covered on that front. You could easily create a JavaScript file that can connect into the SignalR channel without any changes to PHP code other than to include the JavaScript in the client’s page.

    You could quite easily create a single script on your server that the client can reference on their page that pulls everything required on to the client’s page. Because the script executes from your own server I don’t think you’ll need to worry about CORS cause it’s not truly ‘cross site scripting’, because the code executing the requests will be ‘owned’ by your server/domain.

    As for authentication, if it’s only your script that needs access to the authentication, I think that means that you can make use of your domain’s authentication too (cookies etc.) I’d have to check up on that one though. Ultimately you’d authenticate them using a HTTPs request to your domain from the included script?

    Here are some useful threads that might help with that one:

Comments are closed.