SignalR in MVC3, timing and start/connect issues?
asp.net-mvc, asp.net-mvc-3, c#-4.0, signalr
Solution
Starting the SignalR connection is not instantaneous. You call to `connection.GetStuff();` may fail if the connection has not yet been established. If you want this code to run after a connection to the hub is established you should use a callback function.
var connection = $.connection.test;
$.connection.hub.start(function(){
// By convention all exposed hub methods start with lowercase
connection.getStuff();
});
Hub Quickstart: https://github.com/SignalR/SignalR/wiki/QuickStart-Hubs
In-depth look at SignalR javascript client: https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client-Hubs
Problem
I am having a really weird issue with MVC3 and signalr.. I have a simple hub; ``` [HubName("test")] public class Test: Hub { public object GetStuff() { return new { dummy = "Test" }; } } ``` And some client-side code; ``` var connection = $.connection.test; connection.start(); connection.getStuff(); ``` This throws an error; TypeError: Object # has no method 'start' If I instead do ``` var connection = $.connection("test"); ``` I get a different error; TypeError: Object # has no method 'getStuff' jquery-1.6.4.min.js:4 POST http://localhost:63021/Controller/test/negotiate 405 (Method Not Allowed) Note its trying to negotiate under the controller for some reason? Is there some specific route I need to register? Some other magic I dont know about? UPDATE So playing a bit with console -- the first version does in fact create an object that has getStuff() which i can call. But signalr throws up because i have to call start() first -- which doesn't exist! The second one creates an object that DOES have start(), but it doesnt have getStuff().. UPDATE 2 Tried doing $.connection.hub.start instead. This seems to work in the console, but not in the page onload.. Possibly start isnt finished before the hub call is made? Is it async?