Sending messages from Server to clients in SignalR 2.0 - c#

I am learning SignalR and it pretty simple. However, I am having a problem sending Messages from Server to client.
My Hub class is as follows:
public class UpdateHub : Hub
{
private readonly UpdateBroadcast _broadcaster;
public UpdateHub() : this(UpdateBroadcast.Instance) { }
public UpdateHub(UpdateBroadcast broadCaster)
{
_broadcaster = broadCaster;
}
}
And I am calling this hub in my broadcaster class like this:
public class UpdateBroadcast
{
private readonly static Lazy<UpdateBroadcast> _instance = new Lazy<UpdateBroadcast>(
() => new UpdateBroadcast(GlobalHost.ConnectionManager.GetHubContext<UpdateHub>()));
private IHubContext _context;
private UpdateBroadcast(IHubContext context)
{
_context = context;
}
public static UpdateBroadcast Instance
{
get { return _instance.Value; }
}
public void UpdatePost(Post post)
{
_context.Clients.All.updatePost(post);
}
}
In my MVC Controller I am calling the UpdatePost method:
public JsonResult AddPost(Post post)
{
UpdateBroadcast broadcaster = UpdateBroadcast.Instance;
Post result = dbFunctions.AddPost(post);
broadcaster.UpdatePost(post);
return Json(new { success = result != null }, JsonRequestBehavior.DenyGet);
}
When I debug the code, I can see that UpdatePost is executed but there is no activity on the client side. My client-side function is like this:
$(function () {
var update = $.connection.updateHub;
update.client.updatePost = function (data) {
alert("called update post");
};
});
I don't seem to understand what is causing this.

Please check below 2 links. I got really helpful with successfully implementation of signalR. Hopefully, this links help you.
https://github.com/vgheri/ChatR
http://www.codeproject.com/Articles/524066/SignalR-Simple-Chat-Application-in-Csharp

Related

Authenticating a Callback URL in a REST API

We are testing Azure Communication Services in a new project. Specifically, we are looking at the Azure Communication Services for Calling documented here and the quick start project found here.
The general pattern to utilize the service is shown in the following code.
public string AppCallbackUrl => $"{AppBaseUrl}/api/outboundcall/callback?{EventAuthHandler.GetSecretQuerystring}"
// Defined the call with a Callback URL
var source = new CommunicationUserIdentifier(callConfiguration.SourceIdentity);
var target = new PhoneNumberIdentifier(targetPhoneNumber);
var createCallOption = new CreateCallOptions(
new Uri(AppCallbackUrl),
new List<MediaType> { MediaType.Audio },
new List<EventSubscriptionType> { EventSubscriptionType.DtmfReceived });
// Initiate the call
var call = await callClient.CreateCallConnectionAsync(
source, new List<CommunicationIdentifier>() { target }, createCallOption, reportCancellationToken)
.ConfigureAwait(false);
// Register for call back events
RegisterToCallStateChangeEvent(call.Value.CallConnectionId);
The example uses a configuration value or hardcoded secret key to authenticate the Callback Url, as shown below.
[Route("api/[controller]")]
[ApiController]
public class OutboundCallController : ControllerBase
{
[AllowAnonymous]
[HttpPost("callback")]
public async Task<IActionResult> OnIncomingRequestAsync()
{
// Validating the incoming request by using secret set in app.settings
if (EventAuthHandler.Authorize(Request))
{
...
}
else
{
return StatusCode(StatusCodes.Status401Unauthorized);
}
}
}
public class EventAuthHandler
{
private static readonly string SecretKey = "secret";
private static readonly string SecretValue;
static EventAuthHandler()
{
SecretValue = ConfigurationManager.AppSettings["SecretPlaceholder"] ?? "h3llowW0rld";
}
public static bool Authorize(HttpRequest request)
{
if (request.QueryString.Value != null)
{
var keyValuePair = HttpUtility.ParseQueryString(request.QueryString.Value);
return !string.IsNullOrEmpty(keyValuePair[SecretKey]) && keyValuePair[SecretKey].Equals(SecretValue);
}
return false;
}
public static string GetSecretQuerystring => $"{SecretKey}={HttpUtility.UrlEncode(SecretValue)}";
}
Is there a better way to do this in a production environment? How can I incorporate ASP.NET Core authentication with a Callback?

How to send new/updated data to all clients on SignalR for NET Core 2.2

I'm not too familiar with signalr2 on asp.net-core pardon me, am trying to create a POC on how to implement a real-time CRUD application with admin and client using signalr2, but am having issues with signalr data push, I keep getting NullReferenceException: Object reference not set to an instance of an object. on this line await Clients.All.SendAsync("BroadcastData", data);
Below is how I setup my Hub using some online examples I looked-up:
public class OddPublisher : Hub
{
private readonly IOddServices _oddService;
public OddPublisher(IOddServices oddService)
{
_oddService = oddService;
}
public async Task BroadcastData()
{
var data = _oddService.ClientQueryOdds();
await Clients.All.SendAsync("BroadcastData", data); //breaks here
}
}
and this is triggered by admin, on submiting and saving the data sucessfully I call the BroadcastData()
public class BaseController : Controller
{
public IOddServices _oddService;
public readonly OddPublisher _publisher;
public BaseController(IOddServices oddService, )
{
_oddService = oddService;
_teamService = teamService;
_publisher = new OddPublisher(oddService);
}
[HttpPost]
public async Task<IActionResult> odd_entry(CreateOdd dto)
{
//somecode here...
var results = _validator.Validate(dto);
if(!results.IsValid)
{
results.AddToModelState(ModelState, null);
return View(dto);
}
_oddService.CreateOddAndTeam(dto);
await _publisher.BroadcastData(); //Breaks
return RedirectToAction(nameof(index));
}
}
Folled all the instructions as adviced in Microsoft Asp.Net Core Signalr Documentation, my Startup has all required sevices added.
here is the client and the JS file,
"use strict";
var connection = new signalR.HubConnectionBuilder().withUrl("/oddPublisher").build();
connection.on("BroadcastData", data => {
console.table(data);
//getAll();
})
connection.start().then(function () {
getAll();
}).catch(function (err) {
return console.error(err.toString());
});
function getAll() {
var model = $('#dataModel');
$.ajax({
url: '/home/GetLatestOddData',
contentType: 'application/html ; charset:utf-8',
type: 'GET',
dataType: 'html',
success: function (result) { model.empty().append(result); }
});
}
Need some help here guys, I still don't know what am doing wrong, I'll really appreciate if I can get any help on this.
Thank you in advance.
It turns out I needed to Inject the IHubContext into my hubs to have access to the clients.

Call Signalr method from Controller .Net Core 2.1

I am trying to call a method in the signalr Hub class from an (ASP.NET Core) MVC Controller, but I cannot find an example online that shows how to.
Note: There are lots of examples using older versions of signalr with the .Net Framework, but none that I can see that show how to do this in .Net Core.
I need to pass an id from the an MVC Action Result directly through to my Hub, without passing the id to the page, and then having to get a client connection back through to the hub.
public class ChatHub : Hub
{
public async Task DoSomething(int id)
{
//// Something in here.
}
}
public class HomeController : Controller
{
private readonly IHubContext<ChatHub> _hubContext;
public HomeController(IHubContext<ChatHub> hubContext)
{
_hubContext = hubContext;
}
public async Task<ActionResult> Index(int id)
{
//// Call the DoSomething method from here, passing the id across.
await _hubContext.Clients.All.SendAsync("AddToGroup", groupId);
}
}
Is there a way to do this please? (Or am I looking at this the wrong way, and is there a better way to achieve the same result?)
Update: If I pass the Id into the view, and then use JavaScript to call the Hub, this then calls the DoSomething method, so I can see it all hangs together correctly, but not when I try to call it in C#.
I think you're misunderstanding how it all works together (which is the same thing I did up until yesterday), the hub code is for the client-side script code to call back into and then action, whereas the IHubContext is used as the strongly typed methods that will be sent to the Client-side
Hub
// This class is used by the JavaScript Client to call into the .net core application.
public class ChatHub : Hub<IChatClient>
{
public static ConcurrentDictionary<string, string> Connections = new ConcurrentDictionary<string, string>();
// As an example, On connection save the user name with a link to the client Id for later user callback
public override Task OnConnectedAsync()
{
var user = Context.User.Identity.Name;
Connections.AddOrUpdate(this.Context.ConnectionId, user, (key, oldValue) => user);
return base.OnConnectedAsync();
}
public override Task OnDisconnectedAsync(Exception exception)
{
// Do something on disconnect.
}
// Add other methods you want to be able to call from JavaScript side in here...
public void SendMessage(int id, string message)
{
// Message doing stuff here.
}
}
ChatClient Interface
// This provides strongly-typed methods that you'll have on the Client side but these don't exist on the server.
public interface IChatClient
{
//So this method is a JS one not a .net one and will be called on the client(s)
Task DoSomething(int id);
Task NotificationUpdate(int id, string message);
}
Controller
public class HomeController : Controller
{
private readonly IHubContext<ChatHub, IChatClient> _hubContext;
public HomeController(IHubContext<ChatHub, IChatClient> hubContext)
{
_hubContext = hubContext;
}
public async Task<ActionResult> Index(int id)
{
// This calls the method on the Client-side
await _hubContext.Clients.All.DoSomething(id);
}
}
You can use the IHubContext to do this:
public class HomeController : Controller
{
private readonly IHubContext<ChatHub> _hubContext;
public HomeController(IHubContext<ChatHub> hubContext)
{
_hubContext = hubContext;
}
public async Task<ActionResult> Index(int id)
{
//// Call the DoSomething method from here, passing the id across.
await _hubContext.Clients.All.SendAsync("DoSomething", id);
}
}
Full docs and examples here.
I used the code here https://stackoverflow.com/a/53062957/6453193 for my HUB connection.
Since the js client-side code was not posted here. Just wanna share how I do it in js.
const connection = new signalR.HubConnectionBuilder()
.withUrl("/chatHub")
.configureLogging(signalR.LogLevel.Information)
.withAutomaticReconnect()
.build();
async function start() {
try {
await connection.start();
console.log("SignalR Connected.");
} catch (err) {
console.log(err);
setTimeout(start, 5000);
}
};
connection.onclose(async () => {
await start();
});
// Trigger if server-side code call it using NotificationUpdate
connection.on("NotificationUpdate", (message) => {
alert(message);
});
// Start the connection.
start();
and my controller notifying the client-side
await _hubContext.Clients.User(User.Identity.Name).NotificationUpdate($"This is an update");

ASP.NET XSOCKETS Messenger Project Does Not Work

I make an asp.net Messenger WebProject using XSockes.
When run this project onopen and onconnected events its happens.
but wen send message does not work.
what is my fault?
Startup Code:
[assembly: OwinStartupAttribute(typeof(XsocketTest1.Startup))]
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseXSockets(true);
}
}
Starter Code:
[assembly: PreApplicationStartMethod(typeof(XsocketTest1.Starter), "Start")]
namespace XsocketTest1
{
public class Starter
{
private static IXSocketServerContainer container;
public static void Start()
{
container = XSockets.Plugin.Framework.Composable.GetExport<IXSocketServerContainer>();
container.Start();
}
}
}
Controller Code:
public class Chat : XSocketController
{
public string UserName { get; set; }
public void ChatMessage(string message)
{
this.InvokeToAll(message, "chatmessage");
}
}
And JavaScript Code:
$(function () {
try{
var controler = new XSockets.WebSocket('ws://localhost:34853', ['chat']);
var conn = controler.controller('chat');
conn.onopen = function () {
}
conn.onconnected = function () {
console.log('socket connected');
conn.controller('chat').chatmessage = function (data) {
console.log(data.Text);
};
};
}
catch(e)
{
alert(e);
}
$(document).ready(function () {
$("#btnSend").click(function () {
try{
conn.invoke('chatmessage', {
Text: 'Calling chatmessage on server and passing a part of the complex object'
});
}
catch(e)
{ alert(e);}
});
});
});
The code looks pretty weird...
Take a look here: http://xsockets.net/docs/4/installing-xsocketsnet
And you will see that you have duplicate startup classes... All you need is the "usexsockets" part if you use owin and win8+ (also mentioned in the docs).
Your JavaScript might work but is very confusing since it seems like you have misunderstood the difference between a connection and a controller in xsockets.

No clients available in SignalR Hub context

I am new to SignalR and am experimenting with setting up a self host service that I can use to send events to other applications. I am having a problem getting a method to be called on all clients. In the code below _context.Clients.All is always empty. Any Ideas?
Test Method:
var server = new SignalRServer();
server.Start("http://localhost:8555");
var hubConnection = new HubConnection("http://localhost:8555");
var proxy = hubConnection.CreateHubProxy("testHub");
var executed = false;
proxy.On("testMethod", () =>
{
executed = true;
});
hubConnection.Start().Wait();
var hub = new TestHubContext(GlobalHost.ConnectionManager.GetHubContext<TestHub>());
hub.Test();
hubConnection.Stop();
server.Stop();
Self host server:
public class SignalRServer
{
private IDisposable _signalR;
public void Start(string url)
{
_signalR = WebApp.Start<SignalRStartup>(url);
}
public void Stop()
{
_signalR.Dispose();
}
}
public class SignalRStartup
{
public static IAppBuilder App = null;
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
Hub:
public class TestHub : Hub
{
}
public class TestHubContext
{
private IHubContext _context;
public TestHubContext(IHubContext context)
{
_context = context;
}
public void Test()
{
if (_context != null)
{
// At this poing _context.Clients.All is always empty
_context.Clients.All.testMethod();
}
}
}
I think your context / client connections are fine. Without further information I'm guessing your problem is that you are closing your connection and server too quickly after calling hub.Test()
Comment out the two .Stop() calls (and stick a Console.ReadLine in there to keep the console open) and see what happens

Categories

Resources