Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

535
Views
websocket interrupted while angular2 project is loading on firefox

I've just started angular 2. I've done an angular2 sample as given in the https://angular.io/guide/quickstart

when I run the project in Firefox using

npm start

command in terminal, the connection get disconnected after output showing once.Error showing like

The connection to ws://localhost:3000/browser-sync/socket.io/?EIO=3&transport=websocket&sid=6YFGHWy7oD7T7qioAAAA was interrupted while the page was loading

Any idea about how to fix this issue ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I don't know how you manage your web socket but you could consider using the following code. This idea is to wrap the web socket into an observable.

For this you could use a service like below. The initializeWebSocket will create a shared observable (hot) to wrap a WebSocket object.

export class WebSocketService {
  initializeWebSocket(url) {
    this.wsObservable = Observable.create((observer) => {
      this.ws = new WebSocket(url);

      this.ws.onopen = (e) => {
        (...)
      };

      this.ws.onclose = (e) => {
        if (e.wasClean) {
          observer.complete();
        } else {
          observer.error(e);
        }
      };

      this.ws.onerror = (e) => {
        observer.error(e);
      }

      this.ws.onmessage = (e) => {
        observer.next(JSON.parse(e.data));
      }

      return () => {
        this.ws.close();
      };
    }).share();
  }
}

You could add a sendData to send data on the web socket:

export class WebSocketService {
  (...)

  sendData(message) {
    this.ws.send(JSON.stringify(message));
  }
}

The last point is to make things a bit robust, i.e. filter received messages based on a criteria and implement retry when there is a disconnection. For this, you need to wrap our initial websocket observable into another one. This way we can support retries when the connection is lost and integrate filtering on criteria like the client identifier (in the sample the received data is JSON and contains a sender attribute).

export class WebSocketService {
  (...)

  createClientObservable(clientId) {
    return Observable.create((observer) => {
      let subscription = this.wsObservable
        .filter((data) => data.sender!==clientId)
        .subscribe(observer);

      return () => {
        subscription.unsubscribe();
      };
    }).retryWhen((errors) => {
      return Observable.timer(3000);
    });
  }
}

You can see that deconnections are handled in this code using the retryWhen operator of observable.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!