Ending the connection in SignalR upon $destroy

I am currently working on a page that is utilizing SignalR, Angular, and Typescript. Upon the destruction of the current $scope (such as during a page change), I make an attempt to disconnect the client from the SignalR server:

Controller.ts

$scope.$on("$destroy", () => Service.Disconnect());

In my frontend service, I have implemented the following function:

Service.ts

public Disconnect() {
    this.hub.disconnect();
}

Upon changing the page, the client successfully disconnects from the hub. This can be confirmed through console logs:

SignalR: Stopping connection.

SignalR: Closing the Websocket.

SignalR: Fired ajax abort async = true.

SignalR: Stopping the monitoring of the keep alive.

This process works flawlessly. However, upon loading a new state and reconnecting, it appears that the old connection is somehow retained, leading to multiple connection attempts. Subsequently, each navigation back and forth causes additional connection attempts.

EDIT

It seems like the stateChanged event is being triggered multiple times, correlating with the number of previous connection setups. This behavior has been mitigated by introducing a simple logic check for "if already connected" upon initial connection establishment, although this workaround is not ideal.

Is there a best practice for handling client disconnection? Should the server/backend hub also manage disconnection processes? Are there any additional steps that should be taken aside from disconnecting the client when the $scope is destroyed?

Answer №1

Can you please explain the reasoning behind not employing this.hub.stop();?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

javascript guide to dynamically update the glyphicon based on value changes

I am attempting to implement an optimal level feature where a glyphicon arrow-up will be displayed if a value falls within the optimum range, and it will switch to a glyphicon arrow-down if the value is lower. <div class="card-body" ng-repeat="item i ...

One way to access the method `.name` in Angular 1.5 is by using the

I have been researching tutorials and documents, but I am having trouble understanding the code provided below. There are two pieces of code from two different files: one is the service file, and the other is the file where the service is imported. Here i ...

Issue with Angular 6 and Chrome: Event listener ($event) occasionally throws the error "unable to access property 'value' of null"

It appears that the buttons are being iterated correctly using ngFor, and upon inspection they have the correct attributes. However, when clicked, the function in the controller sometimes claims the parameter is 'undefined', happening randomly ab ...

utilizing routerLinks to transfer data and trigger a specific function

I'm having trouble passing data through the routerLink and calling the function(). It doesn't seem to be working as expected. Here's an example of my code and you can view a working demo on StackBlitz. First Component(HTML) <span [route ...

Loading AngularJS controllers and views dynamically using SystemJS allows for a more efficient and streamlined

Is there a method to organize AngularJS in a modular fashion that can dynamically load or require controllers and views based on page routes using SystemJS? UPDATE: Here is my current attempt at utilizing SystemJS app.js - This serves as the primary fil ...

Unlocking the Power of AngularJS through Deferred and Promise for Maximum Reus

I am new to using deferred promises and I couldn't find specific answers to my questions. Is it recommended to use deferred promises even for a single simple ajax($http angularjs) call? I know it is beneficial if we have multiple ajax calls in seq ...

Discovering a variable within an enzyme wrapper for the locate function

Struggling through testing with jest + enzyme. I have an array called OptionsArray that contains options mapped to buttons in a component. In my testing suite for the component, I attempted to do the following: import React from 'react'; import { ...

Utilize the findIndex method to search for an element starting at a designated index

Below is a snippet of code that I have: private getNextFakeLinePosition(startPosition: number): number{ return this.models.findIndex(m => m.fakeObject); } This function is designed to return the index of the first element in the array that has ...

Using the return statement to set a value from a callback function in TypeScript

As I retrieve data from an array of class People, each person has an attendance list represented by Observable<any[]>. // Defining the Person class class Person { id: number; name: string; attendance: Observable<any[]>; // Represents ...

Textarea not displaying default values when ng-model is applied

My Angular application includes a textbox in the following format: <div class="panel-body text-center"> <textarea id="mytext" class="form-control" rows="4">John,2 Jane,3 John,4 Jane,5 </textarea> </ ...

The Angular Material Datepicker lacks any selected date

I am currently in the process of developing a web application using Angular and incorporating Angular Material for certain UI components. I have encountered an issue that I am unable to resolve. When attempting to use the datepicker as outlined on https:// ...

Is MongoDB still displaying results when the filter is set to false?

I am currently trying to retrieve data using specific filters. The condition is that if the timestamp falls between 08:00:00 and 16:00:00 for a particular date, it should return results. The filter for $gte than 16:00:00 is working correctly, but the $lte ...

Is it possible to modify the CSS styling in React using the following demonstration?

I am attempting to create an interactive feature where a ball moves to the location where the mouse is clicked. Although the X and Y coordinates are being logged successfully, the ball itself is not moving. Can anyone help me identify what I might be overl ...

Exploring the Method of Utilizing JSON Attribute in typeScript

How to access JSON attributes in TypeScript while working on an Angular Project? I'm currently in the process of building an Angular project and I need to know how to access JSON attributes within TypeScript. test:string; response:any; w ...

Exploring the world of Angular's HttpClient Requests and Responses

As I delve into the world of signals, I find myself immersed in tutorials and articles on the topic. When it comes to calling an API endpoint using httpClient, I have come across two main approaches that caught my interest. Surprisingly, there isn't m ...

What is the best way to set up an empty {[key: string]: string} object in TypeScript?

interface a { d: {[key: string]: string} } class a { d = {} } The error message returned is as follows: Subsequent property declarations must have the same type. Property 'd' must be of type '{ [key: string]: string; }', but ...

Transferring functionality from a child component to a parent component

I have a Base Component called ListComponent and a ParentComponent named Businesses2ListComponent. The concept is to have multiple types of Lists with tables that all stem from the ListComponent. This requires passing the correct service accordingly. In t ...

Error occurred when trying to import an external module using an invalid hook call

I am creating a package named "Formcomponent" using React and React Bootstrap. This code is from index.tsx /** * Renders a component for a form. */ import React from "react"; import Form from "react-bootstrap/Form"; /** * List of props * @returns */ ...

including identical item in the array

<!DOCTYPE html> <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css"> <body> <script> var app = angul ...

Unable to perform a union operation that combines multiple enums into one

Here is a basic example: export enum EnumA { A = 'a', } export enum EnumB { A = 'b', } export class SomeEntity { id: string; type: EnumA | EnumB; } const foo = (seArray: SomeEntity[]) => { seArray.forEach((se) => { ...