rodgc / ngx-socket-io

Socket.IO module for Angular
MIT License
261 stars 89 forks source link

Initial connect event does not fire #167

Closed brodenbrett closed 2 months ago

brodenbrett commented 9 months ago

When a connection is made to the WebSocket server, the initial connect event does not fire. If the server or client is disconnected and then re-connects, subsequent connect events do fire.

The client is still connected and can receive other events from the server despite the connect event never having fired. EG:

import { Socket } from 'ngx-socket-io';

export class AppComponent {
    constructor(private Socket: Socket) { }

    ngOnInit() {
        const socket = this.Socket.connect();

        this.Socket.fromEvent('connect', () => {
            console.log('connect'); // does not fire until a re-connection
        });

        this.Socket.on('connect', () => {
            console.log('connect'); // does not fire until a re-connection
        });

        this.Socket.fromEvent('disconnect', () => {
            console.log('disconnect'); // works as expected
        });

        this.Socket.fromEvent('RandomEvent', () => {
            console.log('RandomEvent'); // works as expected
        });
    }
}

Note that using the equivalent code with the socket.io-client plugin works as expected, which implies an issue within the plugin rather then the server or network:

import { io } from "socket.io-client";

export class AppComponent {
    constructor() { }

    ngOnInit() {
        const socket = io("ws://example.ca");

        socket.on('connect', () => {
            console.log('connect'); // works as expected
        });

        socket.on('disconnect', () => {
            console.log('disconnect'); // works as expected
        });

        socket.on('RandomEvent', () => {
            console.log('RandomEvent'); // works as expected
        });
    }
}

Socket.IO Server version: v4.6.2 ngx-socket-io version: v4.6.1 Angular version: 17.0.4

Possible related issues here and here.

brodenbrett commented 9 months ago

To anyone who comes across this looking for a solution - I ended up moving forward using the socket.io-client package.

syedhannan commented 9 months ago

@brodenbrett is rxjs for web sockets better than socket.io-client?

brodenbrett commented 9 months ago

@brodenbrett is rxjs for web sockets better than socket.io-client?

I had a very simple implementation; a user connects, joins a room, and listens for a single event with no data. For my purposes there wasn't a stark difference between the two packages, I can't speak to more complex implementations.

That said, ngx-socket-io is easier to implement since you inject a service into your components, and you're only ever dealing with a single socket object/connection. If you use socket.io-client you'll likely have to create your own service. I'd consider ngx-socket-io the preferred way to do things.

theophane-girard commented 7 months ago

any news ?

TomieAi commented 7 months ago

I have question about ngx-socket.io design does it have to be inside service? can i just use the socket anywhere on any component? all of those will still act in the same socket right?

github-actions[bot] commented 3 months ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 2 months ago

This issue was closed because it has been inactive for 7 days since being marked as stale.