I have an MVC web application that communicate to a back-end web API. The API is not exposed directly to public and resides inside a virtual network.
But I want to implement a SignalR connection between my front-end MVC and back end web api.
In this setup I have two SignalR clients and two SignalR servers.
The first SignalR client is a JavaScript client that communicate to the SignalR server in the MVC application.
The second SignalR client is a .NET SignalR client inside the MVC app that can connect to SignalR server in the API.
The setup works this way.
1.JavaScript client establish a connection with MVC SignalR server hub.
2.The MVC SiganlR server hub establish a connection with API server hub through the SignalR .NET client inside the MVC app.
This is my JavaScript Client.
"use strict"; var connection = new signalR.HubConnectionBuilder().withUrl("/mvcHub").build(); connection.on("ReceiveMessage", function (user, message) { var msg = message.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"); var encodedMsg = user + " says " + msg; var li = document.createElement("li"); li.textContent = encodedMsg; document.getElementById("messagesList").appendChild(li); }); connection.start().catch(function (err) { return console.error(err.toString()); }); document.getElementById("sendButton").addEventListener("click", function (event) { var user = document.getElementById("userInput").value; var message = document.getElementById("messageInput").value; connection.invoke("SendMessage", user, message).catch(function (err) { return console.error(err.toString()); }); event.preventDefault(); });
This is my MVC Server hub
using System.Threading.Tasks; using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR.Client; namespace WebApplication2 { public class MvcHub:Hub { private readonly IHubContext<MvcHub> _hubContext; public MvcHub(IHubContext<MvcHub> hubContext) { _hubContext = hubContext; } public async Task SendMessage(string user, string message) { var connection = new HubConnectionBuilder() .WithUrl("http://localhost:50735/apiHub") .Build(); connection.StartAsync().Wait(); await connection.InvokeAsync("SendMessage", "sadsd", "sdf"); connection.On<string, string>("ReceiveMessage", (user1, message1) => { _hubContext.Clients.All.SendAsync("ReceiveMessage", user1, message1); }); } } }
This is my API Server hub
using System.Threading.Tasks; using Microsoft.AspNetCore.SignalR; namespace WebApplication1 { public class ApiHub : Hub { public async Task SendMessage(string user, string message) { await Clients.Caller.SendAsync("ReceiveMessage", user, message); } } }
From inside your API controller you can route message to MVC UI via this code.
public class ValuesController : ControllerBase { private readonly IHubContext<ApiHub> _hubContext; public ValuesController(IHubContext<ApiHub> hubContext) { _hubContext = hubContext; } // GET api/values/5 [HttpGet("{id}")] public ActionResult<string> Get(int id) { _hubContext.Clients.All.SendAsync("ReceiveMessage", "vivek", "asdad"); return "value"; } }
You must be logged in to post a comment.