Here is the quote from plugin's official github repo
The online event fires when a previously unconnected device receives a
network connection to allow an application access to the Internet. It
relies on the same information as the Connection API, and fires when
the connection.type changes from NONE to any other value.
As you can see onConnect will only emit something when previously unconnected device receives a network connection.
To check on startup if device is online you can directly check this.network.type
Or
You can create a service which will handle all these
@Injectable()
export class MyNetworkService implements OnInit {
public connection: BehaviorSubject<string> = new BehaviorSubject(null);
constructor(private network: Network, private platform: Platform) {
this.connection = this.network.type;
this.platform.ready().then(() => {
this.network.onConnect().subscribe(() => {
this.connection = 'something';
});
this.network.onDisconnect().subscribe(() => {
this.connection = 'none';
});
});
}
ngOnInit() {
this._setCurrentConnectionType();
this.platform.ready().then(() => {
this.network.onConnect().pipe(timer(3000)).subscribe(this._onConnectHandler);
this.network.onDisconnect().pipe(timer(3000)).subscribe(this._onDisconnectHandler);
});
}
private _onConnectHandler= () => this.connection.next(this.network.type);
private _onDisconnectHandler = () => this.connection.next('offline');
}
And then you can inject your service where-ever you want and subscribe to connection.
constructor(private myNetworkService: MyNetworkService) {
this.myNetworkService.connection.subscribe(type => {
// you might filter out nulls
// Before subscribe add .pipe(filter(type => type !== null))
})
}