rodgc / ngx-socket-io

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

query data is lost only in production mode #20

Closed achasoft closed 4 years ago

achasoft commented 5 years ago

hi, first thanks for your work :)

i have a angular application integrated with your package, it has the following configuration in root module: SocketIoModule.forRoot({ url: environment.socket.url, options: { query: { userId: (<any>window).MaxDuel.instance.userId } } })

it works like charm only in development mode :D as soon as i ng build my app --prod the user id becomes null production development

rodgc commented 5 years ago

Hello @achasoft Never had this issue before, let me check what maybe causing it.

rodgc commented 5 years ago

Hi @achasoft

I try to reproduce your issue but i was unable.

Do the following

// imports
const config: SocketIoConfig = { 
 url: environment.socket.url,
 options: {
  query:  (<any>window).MaxDuel.instance.userId
 }
};
...
@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...
    SocketIoModule.forRoot(config)
  ],
  bootstrap: [AppComponent]
})

if this don't solve your issue, let me know

orange1337 commented 5 years ago

Have the same issue with query param.

orange1337 commented 5 years ago

Found only one working solution for me:

// app.module.ts
...
const config: SocketIoConfig = { url: '/', options: { autoConnect: false } };
...
// myCustomService.ts
...
import * as io from 'socket.io-client';
import { Socket } from 'ngx-socket-io';
...
@Injectable()
export class MyCustomService {
   constructor(private socket: Socket) {}
   ...
   socketConnect(){
       this.socket.ioSocket.io.opts = {
           query: { account: (<any>window).localStorage.getItem('account') },
           path: '/socket.io/',
           autoConnect: true,
           reconnection: true,
           reconnectionDelay: 1000,
           reconnectionDelayMax : 5000,
           reconnectionAttempts: 10,
           transports: ['polling', 'websocket']
       };
       this.socket.ioSocket.io.uri = (<any>window).location.origin;
       this.socket.connect();
   }
   ...
}
// app.component.ts
 ...
   ngOnInit() {
      this.myCustomService.socketConnect();
  }
...
ctfrancia commented 4 years ago

If this issue is still relevant please open up a new one since it is 2 years old