locked
SignalR With Different types of clients RRS feed

  • Question

  • User1860705008 posted

    I have a web application that needs to communicate with devices.

    So I created a console app and created a hub proxy inside it so that Whenever it receives data from the device it can call my hub method and then my hub will call a javascript method to update the detail on the page.

    Now the issue is... I want to call  console method from hub when user clicks on some control on web page. As I think.. I have 2 clients(one is javascript(website) and other is a console app). So How can I send method call to only console app.

    Then another issue....

    This data that are received from console app to hub will be displayed on web page according to user and machines(depends on which user has selected which machine to work with and there can be many user on website that are using the same machine).

    SO for example I have users A, B C, D and Machines X,Y,z.

    IF user A and B have selected machine X and click on any control, then when the data received from machine X should be updated to only A and B.

    How is all this scenario will be made possible?

    MY Questions -

    1. How can I only send Method call to one client application(console app)
    2. How will I update data to only selected user?
    Wednesday, November 7, 2018 8:12 AM

All replies

  • User61956409 posted

    Hi usha2116,

    Welcome to ASP.NET forums.

    I want to call  console method from hub when user clicks on some control on web page. As I think.. I have 2 clients(one is javascript(website) and other is a console app). So How can I send method call to only console app.

    A simple approach is that you can add all console app clients in a group and add all web clients in another group, and then you can push/send message to the group of console clients from your hub method. 

    For detailed information about "Working with Groups in SignalR", please check the link.

    Approach 2 is that you can define a special method on your console client (not define method with same method name on your JavaScript client), and then you can call that special client method from your hub to broadcast message to console client.

    To define a method that the server can call, use the proxy's On method to register an event handler.

    I have users A, B C, D and Machines X,Y,z.

    IF user A and B have selected machine X and click on any control, then when the data received from machine X should be updated to only A and B.

    You can map and retain user-ConnectionId-machine relationships information, and then you can push message to specified user via ConnectionId.

    For more information about "Mapping SignalR Users to Connections", please check the link.

    With Regards,

    Fei Han

    Thursday, November 8, 2018 2:40 AM
  • User1860705008 posted

    Hi Fei Han,

    I was also thinking the same. But as I dug it some more I realized I dont need user connection Ids I think. Because the updates on webpage will be done using Javascript. And Javascript will update on every page according to specific condition whether its user x y or z.

    Like user x and Y are using machine A so javascript function will check and if it gets that the page  is selected to work on machine A it will automatically update on x and Y and at the same time if User Z is working on machine B then javascript function will update on page that is related to machine B.. (working on machine here means that the data is sent and received to and from that machine using web page). Correct me If I am wrong.

    And yes... The Group thing is right. I really need to make Group for console app and java script .

    I tried your approach 2 to define a special method in Console. But Hub is not calling that Method. I read that to call console app from server that is in another app I need to get Global Context. Right now I can call hub (hub is on my web app) from console app, but vice versa is not working. I will try that to work first.  I will try with groups and global Context also which ever works.

    Monday, November 12, 2018 1:09 AM
  • User61956409 posted

    Hi usha2116,

    Because the updates on webpage will be done using Javascript. And Javascript will update on every page according to specific condition whether its user x y or z.

    As I mentioned, if you map user-ConnectionId-machine relationships information, you can push message to only specified user(s) via ConnectionId(s). Of course, you can also push messages to all connected clients and update UI according to specific condition whether its user x y or z.

    I tried your approach 2 to define a special method in Console. But Hub is not calling that Method.

    The approach works for me, I defined newMessage for console client and broadcastMessage method for JavaScript client, and then call the methods from hub, which work as expected. 

    Console Client:

    static void Main(string[] args)
    {
        Console.WriteLine("Chat Room Console Client Started!");
    
        var connection = new HubConnection("http://localhost:60416/");
        var myHub = connection.CreateHubProxy("chatHub");
    
    
        connection.Start().ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                Console.WriteLine("There was an error opening the connection:{0}",
                                    task.Exception.GetBaseException());
            }
            Console.Write("Enter your message:");
            while (true)
            {
                var message = Console.ReadLine();
                myHub.Invoke("Send", "Console Client1", message).ContinueWith(_task =>
                {
                    if (_task.IsFaulted)
                    {
                        Console.WriteLine("There was an error calling send: {0}", _task.Exception.GetBaseException());
                    }
                });
    
                myHub.On<HubMes>("newMessage", mes =>
                {
                    Console.WriteLine("{0}: {1}", mes.name, mes.message);
                });
            }
        }).Wait();
              
        connection.Stop();
    
        Console.ReadLine();
    }

    JavaScript Client:

    // Declare a proxy to reference the hub.
    var chat = $.connection.chatHub;
    // Create a function that the hub can call to broadcast messages.
    chat.client.broadcastMessage = function (name, message) {
        // Html encode display name and message.
        var encodedName = $('<div />').text(name).html();
        var encodedMsg = $('<div />').text(message).html();
        // Add the message to the page.
        $('#discussion').append('<li><strong>' + encodedName
            + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
    };

    In Hub:

    //push message to javascript clients
    Clients.Others.broadcastMessage(name, $"{message} (for javascript clients)");
    
    //push message to console clients
    Clients.Others.newMessage(new HubMes(name, message+ "(for console clients)"));

    Test Result:

    With Regards,

    Fei Han

    Monday, November 12, 2018 3:00 AM