Search code examples
typescriptsignalraspnetboilerplateasp.net-boilerplate

ReceiveMessage does not work with abp.signalR


I'm using abp.signalR but I've encountered problem-related to ReceiveMessage event, I wanna know once the message is received I tried below code but nothing happened :\

this.chatHub.on("ReceiveMessage", (username: string, message: string) => { 


});

Web console screen shoot:

enter image description here

My back-end code: Do I need to add more code to the backend ?


namespace HealthMapControlPanel.ChatAppService
{
   public class MyChatHub : Hub, ITransientDependency
    {
         public IAbpSession AbpSession { get; set; }

    public ILogger Logger { get; set; }

    public MyChatHub()
    {
        AbpSession = NullAbpSession.Instance;
        Logger = NullLogger.Instance;
    }

    public async Task SendMessage(string message)
    {
        await Clients.All.SendAsync("getMessage", string.Format("User {0}: {1}", AbpSession.UserId, "the message that has been sent from client is"+message));
    }
        public async Task SendToUser(string msg, long userId)
        {
            if (this.Clients != null)
            {
                await Clients.User(userId.ToString()).SendAsync("Send", msg, "From Server by userID ", Context.ConnectionId, Clock.Now);
            }
            else
            {
                throw new UserFriendlyException("something wrong");
            }
        }


        public override async Task OnConnectedAsync()
    {
        await base.OnConnectedAsync();
        Logger.Debug("A client connected to MyChatHub: " + Context.ConnectionId);
    }

    public override async Task OnDisconnectedAsync(Exception exception)
    {
        await base.OnDisconnectedAsync(exception);
        Logger.Debug("A client disconnected from MyChatHub: " + Context.ConnectionId);
    }


    }


}


Solution

  • This is not working because you are subscribing the "ReceiveMessage" on the client and on the server you are doing .SendAsync("Send");

    You should change on the client to:

    this.chatHub.on("Send", (username: string, message: string) => { 
    });
    

    Or on the server:

    await Clients.User(userId.ToString()).SendAsync("ReceiveMessage", msg, "From Server by userID ", Context.ConnectionId, Clock.Now);
    

    Update:

    this.chatHub.on("getMessage", (username: string, message: string) => { 
    });