0

I am running SignalR 2.x in a .Net Framework 4.7 WebAPI app. I have it working to the point where my UI can connect, pass in a message and get a return message. The connection is made and an initial exchange succeeds. However, after that, any time I try and send a message from the server nothing happens. I set a breakpoint in the hub and see that it gets hit. But no message goes out. I need to see if the message is actually being set on the hub. I know the UI does not receive anything, how can I see if .NET actually sends the message?

Hub Contents

using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using System.Diagnostics;
using System.Threading.Tasks;

namespace ShopAP.API.Hubs
{
    [HubName("messages")]
    public class MessageHub : Hub
    {
        public MessageHub()
        {
            Debug.WriteLine("MessageHub");
        }

        public override Task OnConnected()
        {
            Debug.WriteLine("Connected: " + Context.ConnectionId);
            return base.OnConnected();
        }

        public void ShowNewMessage(string message)
        {
            Debug.WriteLine("Show Message: " + message);
            Clients.All.showMessageOnPage("Da message: " + message);
        }

        public void GotMessage(string message)
        {
            Debug.WriteLine(message);
        }
    }
}

Vue.js code

Plugin that makes the connection and passes the message to all components via the event bus.

/* eslint-disable no-console */
import { hubConnection } from 'signalr-no-jquery';

export default {
    install(Vue) {
        const connection = hubConnection('https://localhost:44312/signalr');
        const hubProxy = connection.createHubProxy('messages');

        // use new Vue instance as an event bus
        const invoiceHub = new Vue()

        // every component will use this.$invoiceHub to access the event bus
        Vue.prototype.$invoiceHub = invoiceHub

        // set up event listeners i.e. for incoming "message" event
        // Forward server side SignalR events through $invoiceHub, where components will listen to them
        hubProxy.on('showMessageOnPage', (message) => {
            invoiceHub.$emit('updateMessage', message);
        });

        connection.logging = true;

        // connect
        connection.start()
        .done(function(){ 
            console.log('Now connected, connection ID=' + connection.id); 
            hubProxy.invoke('showNewMessage', 'from front end'); 
        })
        .fail(function(){ console.log('Could not connect'); });
    }
}

In one of the components...

created() {
  this.$invoiceHub.$on("updateMessage", this.showMessage);
},

And the method that just proves I'm getting data from the server:

showMessage(message) {
  // eslint-disable-next-line no-console
  console.log(message);
  alert("Chips: " + message);
  this.message = message;
},
Connie DeCinko
  • 802
  • 2
  • 15
  • 32
  • Post some code for the method in the hub. It may not be hub related but rather what you are doing within the method. A simple "try catch" is good practice if not in place. – Frank M Dec 11 '20 at 13:17
  • Just posted my Hub. I see the output of the Debug lines. However the message does not appear to go anywhere, the client never sees it. – Connie DeCinko Dec 11 '20 at 14:38
  • What about the client? If you get a connection and message once and nothing after then it may be in regards to where your have your showMessageOnPage function. – Frank M Dec 11 '20 at 16:38
  • @FrankM I am able to get and display the initial message when the connection starts. After that, nothing. UI code above. – Connie DeCinko Dec 11 '20 at 17:55
  • Other than within in the connection.start().done(function) where are you calling - hubProxy.invoke('showNewMessage', 'from front end'); ? Sorry, cannot see where you are calling into the hub other than the one time after connection is done. – Frank M Dec 11 '20 at 18:08
  • Added that code above. Just basic, throws the message as an alert and displays on the page to show that I am getting back server data. – Connie DeCinko Dec 11 '20 at 18:40
  • 1
    function names must be matched. you are not listening `showMessageOnPage` in FE. And probably you are able to see the push in the browser console – ilkerkaran Dec 11 '20 at 18:43
  • There is nothing in the browser console. Which is what makes me think nothing is being sent from SignalR. – Connie DeCinko Dec 11 '20 at 20:01

0 Answers0