             }
         }
         
-        let args = jsonArguments.joinWithSeparator(",")
-        let js = "swiftR.hubs.\(name).invoke('\(method)', \(args))"
+        let args = jsonArguments.joinWithSeparator(", ")
+        
+        let uuid = NSUUID().UUIDString
+        if let handler = callback {
+            invokeHandlers[uuid] = handler
+        }
+        
+        let doneJS = "function() { postMessage({ message: 'invokeHandler', hub: '\(name.lowercaseString)', id: '\(uuid)', result: arguments[0] }); }"
+        let failJS = "function() { postMessage({ message: 'invokeHandler', hub: '\(name.lowercaseString)', id: '\(uuid)', error: arguments[0].message }); }"
+        let js = "swiftR.hubs.\(name).invoke('\(method)', \(args)).done(\(doneJS)).fail(\(failJS))"
         
         connection.runJavaScript(js)
     }

        connection.runJavaScript("addHandler('\(name)', '\(method)', \(p))")
    }
    
    public func invoke(method: String, arguments: [AnyObject]?) {
        var jsonArguments = [String]()
        
        if let args = arguments {

public class Hub {
    let name: String
    var handlers: [String: AnyObject? -> ()] = [:]
    var invokeHandlers: [String: (AnyObject?, String?) -> ()] = [:]
    
    public let connection: SignalR!
    
