var connection = new signalR.HubConnectionBuilder().withUrl(config.hubUrl).build(); var connectionId; function connect() { if (connection.state === signalR.HubConnectionState.Disconnected) { connection.start().then(function () { console.log('signalr connected'); }).catch(function (err) { console.log(err); setTimeout(connect, 5000); }); } } connection.onclose(function () { console.log('connect closed'); connect(); }); connection.on('Connected', function (id) { connectionId = id; PubSub.publish('Connected'); }); connection.on("ServerToClient", function (method, message, to, from) { console.log(method); PubSub.publish(method, { message: message, to: to, from: from }); }); connect();