ZedBee
ZedBee

Reputation: 2378

Connection must be started before data can be sent. Call .start() before .send()

I have the following code to call a method on hub from client page

var app = $.connection.myHub;    
$.connection.hub.start().done(function () {         
            $('#mylink').click(function (e) {
                e.preventDefault();

                app.server.myMethod();
            });
    ).fail(function (error) {
            console.log('Invocation of start failed. Error: ' + error)
        });

This code works fine but after some time is passed, when I click the link again, I get the following error

SignalR: Connection must be started before data can be sent. Call .start() before .send()
  1. The link click event is already inside the start().done() so if the connection is not started how does the event handler for link click is even triggered.

  2. If this is because of connection timeout how can I handle the exception and redirect the user to login page.

  3. Why does the code inside the .fail() does not execute

Upvotes: 2

Views: 7675

Answers (1)

Michal Levý
Michal Levý

Reputation: 37853

Recommended reading: Connection Lifetime Events in SignalR

This is my theory about whats happening. You can confirm it by turning on client side logging - add $.connection.hub.logging = true; before start() call.

  1. and 3. Connection is probably started ok - as you say This code works fine but after some time.... For same reason, .fail() is not executed.

But after connection is established, there can be short or longer disconnects. Client is trying to reconnect for some time (disconnect timeout) and if not successful, it'll give up and you need to call .start() again (in disconnected handler for example).

  1. By handling 'disconnected' event

    $.connection.hub.disconnected(function() {});

Upvotes: 3

Related Questions