locked
SignalR: Connection must be started before data can be sent. Call .start() before .send() RRS feed

  • Question

  • User81789783 posted

    i have chat app running ,its WORKING fine ,after some time i used chat app and the project is still running ,but its give error on console.please let me know if i need to share code stuff etc etc

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

    Saturday, June 1, 2019 11:18 AM

All replies

  • User61956409 posted

    Hi erum,

    erum

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

    I can reproduce same issue if I call hub method before I started the connection from SignalR JavaScript client. Please check your JavaScript client code and make sure you started the connection before you call hub method. 

    Besides, if you can share a reproducible sample, we can try to check it and help find the issue.

    With Regards,

    Fei Han

    Monday, June 3, 2019 3:33 AM
  • User765422875 posted

    The connection and hub proxy must be initialized before starting a connection. You should read the tutorial carefully as well.

    $(function () {
            // Declare a proxy to reference the hub. 
            var connection = $.hubConnection('http://www.somesite.net/');
            var chat = connection.createHubProxy('YourHub');
    
            connection.start().done(function() {
                console.log('Now connected, connection ID=' + connection.id); 
                // Wire up Send button to call sendmessage on the server.
                $('#sendmessage').click(function () {
                    chat.invoke('method1', '0000');
                    });
                })
                .fail(function(){ console.log('Could not connect'); });;
        });

    Monday, June 3, 2019 3:00 PM
  • User81789783 posted

    well fei han ,what actually is happening is this , I run my code (signal R chat App ).and its working fine in diff browsers. if i give certain pause say 5 min or so . it gives message like .despite application is running ,

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

    Monday, June 10, 2019 1:28 PM
  • User81789783 posted

    i am pretty  sure there is certain timeout that need to be set

    Monday, June 10, 2019 1:31 PM
  • User765422875 posted

    No, its about handling connection lifetime events. You should read through this entirely.

    https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/handling-connection-lifetime-events

    Bottom line, SignalR does not handle disconnections. You need to handle them by yourself.

    When your client loses connection, SignalR will try to reconnect for about 20 seconds. The state is reconnecting.  After that period, it goes to disconnected state and won´t connect ever again.

    To handle this situation you must listen to connection state changes, and reconnect yourself when state == disconnected.

    Monday, June 10, 2019 2:26 PM
  • User81789783 posted

    so deepal , is there any way to continue remain connected  as long as application is running .

    Thanks

    Tuesday, June 11, 2019 3:33 AM
  • User61956409 posted

    Hi erum,

    If you'd like to automatically re-establish a connection after it has been lost and the attempt to reconnect has timed out, you can refer to ["How to continuously reconnect"](https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/handling-connection-lifetime-events#how-to-continuously-reconnect).

    $.connection.hub.disconnected(function() {
       setTimeout(function() {
           $.connection.hub.start();
       }, 5000); // Restart connection after 5 seconds.
    });

    With Regards,

    Fei Han

    Tuesday, June 11, 2019 9:03 AM
  • User81789783 posted

    yes i merge below code and put alert ,so that it msg each after 5 sec ,but its not

      $.connection.hub.disconnected(function () {
                        setTimeout(function () {
                            $.connection.hub.start();
                        }, 5000); // Restart connection after 5 seconds.
                        alert ("Reconned")
                    });

    Tuesday, June 11, 2019 12:31 PM
  • User61956409 posted

    Hi erum,

    yes i merge below code and put alert ,so that it msg each after 5 sec ,but its not

    I did a test with following code snippet in my SignalR application, which work for me, the code could be executed in disconnected event handler on JavaScript client when the server or the physical connection are unavailable.

    $(function () {
        // Reference the auto-generated proxy for the hub.
        var chat = $.connection.chatHub;
    
        // code logic here
    
        $.connection.hub.start().done(function () {
            // code logic here
        });
    
        $.connection.hub.disconnected(function () {
            setTimeout(function () {
                $.connection.hub.start();
                console.log("restart connection");
            }, 5000); // Restart connection after 5 seconds.
        });
    
    });

    You include too many functionalities/code logic in your existing SignalR application, if possible, you can create a new SignalR application to test whether above code can work fine. 

    With Regards,

    Fei Han

    Monday, June 17, 2019 7:53 AM