Skip to content

New CLI app doesn't register service worker if socket.io-client is used #9098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
arknotts opened this issue Jan 5, 2018 · 5 comments
Closed
Assignees
Labels
needs: investigation Requires some digging to determine if action is needed

Comments

@arknotts
Copy link

arknotts commented Jan 5, 2018

Versions

@angular/cli: 1.6.3
node: 8.9.3
npm: 5.5.1

Repro steps

  • Step 1: Initialize new CLI app (ng new ngsw-test --service-worker)
  • Step 2: npm install socket.io-client --save and npm install "@types/socket.io-client" --save-dev
  • Step 3: In the default component, initialize a new socket:
import { Component } from '@angular/core';
import * as socket from 'socket.io-client';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  private socket: SocketIOClient.Socket;

  constructor() {
	  //Comment this out and the service worker gets registered fine
	  this.socket = socket('//localhost:8081', {
	  	path: '/ws'
	  });
  }
}

Observed behavior

The new CLI app doesn't register the service worker (if the socket initialization in app.component.ts is commented out it works fine). No errors in the console (other than connection refused if the socket server isn't running. I get the same behavior if it connects fine though).

Desired behavior

Register the service worker even when a socket is being used.

Mention any other details that might be useful (optional)

@clydin clydin added the needs: investigation Requires some digging to determine if action is needed label Jan 5, 2018
@arknotts
Copy link
Author

An update to this: it works fine if I initialize the socket with autoConnect false like so:

this.socket = socket('[url]', { autoConnect: false });

Even if I setTimeout for several seconds before trying to connect, it appears to wait for the socket to be initialized and then subsequently doesn't load ngsw-worker.js. The only way I can get the service worker to stay is if I connect to the socket on a UI action (like a button press).

@skydever
Copy link

skydever commented Mar 6, 2018

maybe this is related to angular #20970, the zone does not get stable because the autoConnect of socket.io is doing a setInterval (check connection, reconnect if broken)?

@JumBay
Copy link

JumBay commented May 29, 2018

I found this workaround:

  constructor(private ngZone: NgZone) {

    this.ngZone.runOutsideAngular(() => {
  
      this.io = io(yourUrl);
    });
  }

@filipesilva
Copy link
Contributor

I think this is the same as #9021. If this is still a problem can you follow up there please?

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Nov 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs: investigation Requires some digging to determine if action is needed
Projects
None yet
Development

No branches or pull requests

6 participants