Skip to content

BUG - Socket class is undefined #197

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

Open
LuisOfCourse opened this issue Apr 17, 2025 · 6 comments
Open

BUG - Socket class is undefined #197

LuisOfCourse opened this issue Apr 17, 2025 · 6 comments

Comments

@LuisOfCourse
Copy link

LuisOfCourse commented Apr 17, 2025

Hello everyone,

When I use the provideSocketIo provider instead of the Angular Module forRoot import, i get Socket is undefined when doing any type of operation on the socket class of ngx-socket-io.

-- app.config.ts

import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';
import { SocketIoConfig, provideSocketIo } from 'ngx-socket-io';
import { routes } from './app.routes';

const config: SocketIoConfig = { url: 'http://localhost:3000', options: {} };

export const appConfig: ApplicationConfig = {
  providers: [
    provideZoneChangeDetection({ eventCoalescing: true }),
    provideRouter(routes),
    provideSocketIo(config)]
};

-- app.component.ts

@Component({
  selector: 'app-root',
  imports: [RouterOutlet, NgFor, AsyncPipe],
  templateUrl: './app.component.html',
  styleUrl: './app.component.scss',
})
export class AppComponent {
  constructor(private socket: ChatService) {}

  sendMessage(msg: string) {
    this.socket.sendMessage(msg)
  }

  getMessage() {
    return this.socket.getMessage()
  }
}

-- chat.service.ts

import { Injectable } from '@angular/core';
import { Socket } from 'ngx-socket-io';
import { map } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class ChatService {
  constructor(private socket: Socket) {
  }

  sendMessage(msg: string) {
    this.socket.emit('message', msg);
  }
  getMessage() {
    return this.socket.fromEvent('message').pipe(map(data => data.msg));
  }
}
@LuisOfCourse
Copy link
Author

After digging into the problem, it seems like the provider offered by this package is broken.
I temporarely fixed it by overwriting the Socket class provider as follows:

export const appConfig: ApplicationConfig = {
  providers: [
    provideZoneChangeDetection({ eventCoalescing: true }),
    provideRouter(routes),
    provideSocketIo(config),
    {
      provide: Socket,
      useFactory: () => {
        const config = inject(SOCKET_CONFIG_TOKEN);
        return new Socket(config);
      }
    }]
};

@alanpurple
Copy link

provideSocketIo is not working, should use old module instead

@alanpurple
Copy link

this should be flagged as bug

@LuisOfCourse LuisOfCourse changed the title Socket class is undefined BUG - Socket class is undefined Apr 22, 2025
@LuisOfCourse
Copy link
Author

you can use the workaround i posted. Really simple.

Copy link

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

@github-actions github-actions bot added the stale label May 23, 2025
@alanpurple
Copy link

Not stale. . .. this bug is critical yet unresolved

@github-actions github-actions bot removed the stale label May 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants