Asked by:
Problems Finding Hub

Question
-
User1122355199 posted
Hello everyone and thanks for your help in advance. I'm trying to develop a SignalR application but am running into problems getting the paths correct. I have a hub that looks like:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Microsoft.AspNet.SignalR; using System.Data.SqlClient; using System.Data; using Microsoft.AspNet.SignalR.Hubs; namespace ChatExample.Hubs { [HubName("TwilioSMS")] public class TwilioSMS : Hub { // public IEnumerable<tbl_Log_InboundSMSMessages> GetAllMessages() { // // Do database stuff to create list and SQL Notification return SMSMessages; } } }
The jQuery on the page:
<script src="~/Scripts/jquery-1.10.2.js"></script> <script src="~/Scripts/jquery.signalR-2.3.0.js"></script> <!--Reference the autogenerated SignalR hub script. --> <script src="/signalr/hubs"></script> <script type="text/javascript"> $(function () { // Declare a proxy to reference the hub. var notifications = $.connection.TwilioSMS; //debugger; // Create a function that the hub can call to broadcast messages. notifications.client.updateMessages = function () { getAllMessages() }; // Start the connection. $.connection.hub.start().done(function () { alert("connection started") getAllMessages(); }).fail(function (e) { alert(e); }); }); function getAllMessages() { var tbl = $('#messagesTable'); $.ajax({ url: '/TwilioSMS/GetAllMessages', contentType: 'application/html ; charset:utf-8', type: 'GET', dataType: 'html' }).success(function (result) { tbl.empty().append(result); }).error(function () { }); } </script>
I am receiving the error "jquery-1.10.2.js:8720 GET http://localhost:51695/TwilioSMS/GetAllMessages 404 (Not Found)". I'm not sure what the problem is. Any help would be appreciated.
Thursday, December 13, 2018 2:29 AM
All replies
-
User61956409 posted
Hi kmcnet,
According to your code, to call the hub method from your JavaScript Client, you can try notifications.server.GetAllMessages().
For more information about how to call server methods from the client, please check these links:
- https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#how-to-call-server-methods-from-the-client
- https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/tutorial-getting-started-with-signalr#set-up-the-project
With Regards,
Fei Han
Friday, December 14, 2018 2:36 AM -
User1122355199 posted
Thanks so much for the response. I was trying to follow an article https://www.codeproject.com/Articles/874240/SignalR-Database-update-notifications-in-ASP-NET-M that demonstrates how to call a partial view rather than populating the updated portions of the page client side. I do have a working version that returns a List<tbl_Log_InboundSMSMessages>. So I can traverse these entries using a javascript loop to populate a table, however, I would like to try returning a rendered partial view in the alternative, but I'm not sure how to do this. Also, not sure which is more efficient. Any suggestions?
Friday, December 14, 2018 8:18 PM -
User61956409 posted
Hi kmcnet,
follow an article https://www.codeproject.com/Articles/874240/SignalR-Database-update-notifications-in-ASP-NET-MI checked the above link, and it makes request to "/home/GetMessages" that should be a controller action (not a hub method) in JavaScript function
getAllMessages()
.I recommend that you can know and get started with ASP.NET SignalR before you build the project.
With Regards,
Fei Han
Monday, December 17, 2018 3:02 AM