I have a MVC 3, VB.NET, Razor app that uses SignalR for a chat and system message system… The chat works flawlessly but I would like to be able to add a function in my admin controller that will send messages to the hub which the hub will then do its normal thing as if the Javascript had called it from a view…The hub is set up as below:
Imports SignalR.Hubs
Imports SignalR
Namespace SingalRTest
Public Class Chat
Inherits Hub
Public Sub Send(ByVal clientName As String, ByVal message As String)
'Call the addMessage method on all clients.
Clients.addMessage(clientName, message)
End Sub
End Class
End Namespace
I thought about simply using NEW but that wont work because as I understand it the instance of the hub has to remain intact..
What i am trying to do is something like this:
Public Function notification(ByVal systemMessage as string)
Dim y As SingalRTest.Chat = Nothing
y.Send(User.Identity.Name.ToString, systemMessage)
Return RedirectToAction("Index", "Admin")
End Function
This will not work at all and errors saying:
Object reference not set to an instance of an Object
when it gets to the y.Send line…
Wow, this took a lot of digging to find on the internet. I simply needed to add a shared sub in my Hub class which now looks like this:
Then anytime I want to send a notification in the window, I would just drop this code in the controller function to call the addMessage method on all clients:
Maybe this will help someone else.