SignalR Core call function when connection is established












0















When my client successfully connects to the hub, I want the client to immediately join a group. I have a method in my hub to do that, I just need an event handler for when the connection is established, just like



connection.start().done(
function () {
connection.invoke('JoinGroup', 'GroupName');
});


in SignalR for plain ASP.Net.



Can I do that or do I have to set a timer to do it after x seconds after the start(); call was made?



Edit:



I found out I can do



connection.start(
function (){
connection.invoke('JoinGroup', 'GroupName');
}
);


but it tells me that it Cannot send data if the connection is not in the 'Connected' State.



what do?










share|improve this question



























    0















    When my client successfully connects to the hub, I want the client to immediately join a group. I have a method in my hub to do that, I just need an event handler for when the connection is established, just like



    connection.start().done(
    function () {
    connection.invoke('JoinGroup', 'GroupName');
    });


    in SignalR for plain ASP.Net.



    Can I do that or do I have to set a timer to do it after x seconds after the start(); call was made?



    Edit:



    I found out I can do



    connection.start(
    function (){
    connection.invoke('JoinGroup', 'GroupName');
    }
    );


    but it tells me that it Cannot send data if the connection is not in the 'Connected' State.



    what do?










    share|improve this question

























      0












      0








      0








      When my client successfully connects to the hub, I want the client to immediately join a group. I have a method in my hub to do that, I just need an event handler for when the connection is established, just like



      connection.start().done(
      function () {
      connection.invoke('JoinGroup', 'GroupName');
      });


      in SignalR for plain ASP.Net.



      Can I do that or do I have to set a timer to do it after x seconds after the start(); call was made?



      Edit:



      I found out I can do



      connection.start(
      function (){
      connection.invoke('JoinGroup', 'GroupName');
      }
      );


      but it tells me that it Cannot send data if the connection is not in the 'Connected' State.



      what do?










      share|improve this question














      When my client successfully connects to the hub, I want the client to immediately join a group. I have a method in my hub to do that, I just need an event handler for when the connection is established, just like



      connection.start().done(
      function () {
      connection.invoke('JoinGroup', 'GroupName');
      });


      in SignalR for plain ASP.Net.



      Can I do that or do I have to set a timer to do it after x seconds after the start(); call was made?



      Edit:



      I found out I can do



      connection.start(
      function (){
      connection.invoke('JoinGroup', 'GroupName');
      }
      );


      but it tells me that it Cannot send data if the connection is not in the 'Connected' State.



      what do?







      signalr.client asp.net-core-signalr






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 13:13









      lennyylennyy

      152117




      152117
























          1 Answer
          1






          active

          oldest

          votes


















          2














          SignalR is notoriously difficult due to version mismatches etc.



          Please see the following article: https://docs.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-2.1&tabs=visual-studio



          In it, there's a section that specifies how to start (and wait for connection to be established):



          var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();
          connection.start().then(() => {
          //try some stuff here :)
          })
          .catch(function (err) {
          //failed to connect
          return console.error(err.toString());
          });


          The javascript client uses promises that have to be resolved/rejected before you can use it.
          The other option is to wrap inside async method and await the call (not sure if this will work correctly). Such as:



          await connection.start();
          //connection should be started now. beware of exceptions though





          share|improve this answer
























          • I tried await connection.start() but it told me that's only for async functions. I'll try the then. thanks for the quick reply.

            – lennyy
            Nov 21 '18 at 13:41






          • 1





            Yeah that's correct, it has to be inside an async function(){} :-). No problem. Good luck!

            – Neal
            Nov 21 '18 at 13:44











          • It's still saying Cannot send data if the connection is not in the 'Connected' State. but it does recieve method calls sent to the client group it's supposed to join. I guess that's good enough.

            – lennyy
            Nov 21 '18 at 13:45






          • 1





            Check out in the hub, there are two methods called OnConnected and OnDisconnected that you can override from inherited hub for troubleshooting.

            – Neal
            Nov 21 '18 at 13:46






          • 1





            "Experience is simply the name we give our mistakes" - Oscar Wilde. :-) So, I suspect that the client that's failing to connect you get some error before it stating failed to negotiate (or something to that effect) The answer is to enable something called CORS. Basic example here: stackoverflow.com/questions/48185985/… I recommend you look into the CORS stuff for ASP core and setup a policy only whitelisting your domain.

            – Neal
            Nov 21 '18 at 14:30











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412852%2fsignalr-core-call-function-when-connection-is-established%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          SignalR is notoriously difficult due to version mismatches etc.



          Please see the following article: https://docs.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-2.1&tabs=visual-studio



          In it, there's a section that specifies how to start (and wait for connection to be established):



          var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();
          connection.start().then(() => {
          //try some stuff here :)
          })
          .catch(function (err) {
          //failed to connect
          return console.error(err.toString());
          });


          The javascript client uses promises that have to be resolved/rejected before you can use it.
          The other option is to wrap inside async method and await the call (not sure if this will work correctly). Such as:



          await connection.start();
          //connection should be started now. beware of exceptions though





          share|improve this answer
























          • I tried await connection.start() but it told me that's only for async functions. I'll try the then. thanks for the quick reply.

            – lennyy
            Nov 21 '18 at 13:41






          • 1





            Yeah that's correct, it has to be inside an async function(){} :-). No problem. Good luck!

            – Neal
            Nov 21 '18 at 13:44











          • It's still saying Cannot send data if the connection is not in the 'Connected' State. but it does recieve method calls sent to the client group it's supposed to join. I guess that's good enough.

            – lennyy
            Nov 21 '18 at 13:45






          • 1





            Check out in the hub, there are two methods called OnConnected and OnDisconnected that you can override from inherited hub for troubleshooting.

            – Neal
            Nov 21 '18 at 13:46






          • 1





            "Experience is simply the name we give our mistakes" - Oscar Wilde. :-) So, I suspect that the client that's failing to connect you get some error before it stating failed to negotiate (or something to that effect) The answer is to enable something called CORS. Basic example here: stackoverflow.com/questions/48185985/… I recommend you look into the CORS stuff for ASP core and setup a policy only whitelisting your domain.

            – Neal
            Nov 21 '18 at 14:30
















          2














          SignalR is notoriously difficult due to version mismatches etc.



          Please see the following article: https://docs.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-2.1&tabs=visual-studio



          In it, there's a section that specifies how to start (and wait for connection to be established):



          var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();
          connection.start().then(() => {
          //try some stuff here :)
          })
          .catch(function (err) {
          //failed to connect
          return console.error(err.toString());
          });


          The javascript client uses promises that have to be resolved/rejected before you can use it.
          The other option is to wrap inside async method and await the call (not sure if this will work correctly). Such as:



          await connection.start();
          //connection should be started now. beware of exceptions though





          share|improve this answer
























          • I tried await connection.start() but it told me that's only for async functions. I'll try the then. thanks for the quick reply.

            – lennyy
            Nov 21 '18 at 13:41






          • 1





            Yeah that's correct, it has to be inside an async function(){} :-). No problem. Good luck!

            – Neal
            Nov 21 '18 at 13:44











          • It's still saying Cannot send data if the connection is not in the 'Connected' State. but it does recieve method calls sent to the client group it's supposed to join. I guess that's good enough.

            – lennyy
            Nov 21 '18 at 13:45






          • 1





            Check out in the hub, there are two methods called OnConnected and OnDisconnected that you can override from inherited hub for troubleshooting.

            – Neal
            Nov 21 '18 at 13:46






          • 1





            "Experience is simply the name we give our mistakes" - Oscar Wilde. :-) So, I suspect that the client that's failing to connect you get some error before it stating failed to negotiate (or something to that effect) The answer is to enable something called CORS. Basic example here: stackoverflow.com/questions/48185985/… I recommend you look into the CORS stuff for ASP core and setup a policy only whitelisting your domain.

            – Neal
            Nov 21 '18 at 14:30














          2












          2








          2







          SignalR is notoriously difficult due to version mismatches etc.



          Please see the following article: https://docs.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-2.1&tabs=visual-studio



          In it, there's a section that specifies how to start (and wait for connection to be established):



          var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();
          connection.start().then(() => {
          //try some stuff here :)
          })
          .catch(function (err) {
          //failed to connect
          return console.error(err.toString());
          });


          The javascript client uses promises that have to be resolved/rejected before you can use it.
          The other option is to wrap inside async method and await the call (not sure if this will work correctly). Such as:



          await connection.start();
          //connection should be started now. beware of exceptions though





          share|improve this answer













          SignalR is notoriously difficult due to version mismatches etc.



          Please see the following article: https://docs.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-2.1&tabs=visual-studio



          In it, there's a section that specifies how to start (and wait for connection to be established):



          var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();
          connection.start().then(() => {
          //try some stuff here :)
          })
          .catch(function (err) {
          //failed to connect
          return console.error(err.toString());
          });


          The javascript client uses promises that have to be resolved/rejected before you can use it.
          The other option is to wrap inside async method and await the call (not sure if this will work correctly). Such as:



          await connection.start();
          //connection should be started now. beware of exceptions though






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 13:29









          NealNeal

          283111




          283111













          • I tried await connection.start() but it told me that's only for async functions. I'll try the then. thanks for the quick reply.

            – lennyy
            Nov 21 '18 at 13:41






          • 1





            Yeah that's correct, it has to be inside an async function(){} :-). No problem. Good luck!

            – Neal
            Nov 21 '18 at 13:44











          • It's still saying Cannot send data if the connection is not in the 'Connected' State. but it does recieve method calls sent to the client group it's supposed to join. I guess that's good enough.

            – lennyy
            Nov 21 '18 at 13:45






          • 1





            Check out in the hub, there are two methods called OnConnected and OnDisconnected that you can override from inherited hub for troubleshooting.

            – Neal
            Nov 21 '18 at 13:46






          • 1





            "Experience is simply the name we give our mistakes" - Oscar Wilde. :-) So, I suspect that the client that's failing to connect you get some error before it stating failed to negotiate (or something to that effect) The answer is to enable something called CORS. Basic example here: stackoverflow.com/questions/48185985/… I recommend you look into the CORS stuff for ASP core and setup a policy only whitelisting your domain.

            – Neal
            Nov 21 '18 at 14:30



















          • I tried await connection.start() but it told me that's only for async functions. I'll try the then. thanks for the quick reply.

            – lennyy
            Nov 21 '18 at 13:41






          • 1





            Yeah that's correct, it has to be inside an async function(){} :-). No problem. Good luck!

            – Neal
            Nov 21 '18 at 13:44











          • It's still saying Cannot send data if the connection is not in the 'Connected' State. but it does recieve method calls sent to the client group it's supposed to join. I guess that's good enough.

            – lennyy
            Nov 21 '18 at 13:45






          • 1





            Check out in the hub, there are two methods called OnConnected and OnDisconnected that you can override from inherited hub for troubleshooting.

            – Neal
            Nov 21 '18 at 13:46






          • 1





            "Experience is simply the name we give our mistakes" - Oscar Wilde. :-) So, I suspect that the client that's failing to connect you get some error before it stating failed to negotiate (or something to that effect) The answer is to enable something called CORS. Basic example here: stackoverflow.com/questions/48185985/… I recommend you look into the CORS stuff for ASP core and setup a policy only whitelisting your domain.

            – Neal
            Nov 21 '18 at 14:30

















          I tried await connection.start() but it told me that's only for async functions. I'll try the then. thanks for the quick reply.

          – lennyy
          Nov 21 '18 at 13:41





          I tried await connection.start() but it told me that's only for async functions. I'll try the then. thanks for the quick reply.

          – lennyy
          Nov 21 '18 at 13:41




          1




          1





          Yeah that's correct, it has to be inside an async function(){} :-). No problem. Good luck!

          – Neal
          Nov 21 '18 at 13:44





          Yeah that's correct, it has to be inside an async function(){} :-). No problem. Good luck!

          – Neal
          Nov 21 '18 at 13:44













          It's still saying Cannot send data if the connection is not in the 'Connected' State. but it does recieve method calls sent to the client group it's supposed to join. I guess that's good enough.

          – lennyy
          Nov 21 '18 at 13:45





          It's still saying Cannot send data if the connection is not in the 'Connected' State. but it does recieve method calls sent to the client group it's supposed to join. I guess that's good enough.

          – lennyy
          Nov 21 '18 at 13:45




          1




          1





          Check out in the hub, there are two methods called OnConnected and OnDisconnected that you can override from inherited hub for troubleshooting.

          – Neal
          Nov 21 '18 at 13:46





          Check out in the hub, there are two methods called OnConnected and OnDisconnected that you can override from inherited hub for troubleshooting.

          – Neal
          Nov 21 '18 at 13:46




          1




          1





          "Experience is simply the name we give our mistakes" - Oscar Wilde. :-) So, I suspect that the client that's failing to connect you get some error before it stating failed to negotiate (or something to that effect) The answer is to enable something called CORS. Basic example here: stackoverflow.com/questions/48185985/… I recommend you look into the CORS stuff for ASP core and setup a policy only whitelisting your domain.

          – Neal
          Nov 21 '18 at 14:30





          "Experience is simply the name we give our mistakes" - Oscar Wilde. :-) So, I suspect that the client that's failing to connect you get some error before it stating failed to negotiate (or something to that effect) The answer is to enable something called CORS. Basic example here: stackoverflow.com/questions/48185985/… I recommend you look into the CORS stuff for ASP core and setup a policy only whitelisting your domain.

          – Neal
          Nov 21 '18 at 14:30




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412852%2fsignalr-core-call-function-when-connection-is-established%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Can I use Tabulator js library in my java Spring + Thymeleaf project?