bougarfaoui / ng-socket-io

Socket.IO module for Angular
MIT License
255 stars 57 forks source link

I want to pass an option with the URL but only after I login #46

Closed murshid1988 closed 6 years ago

murshid1988 commented 6 years ago

Hi,

So this is the thing. I would like to pass the userID as an option while connecting to socket server. So I won't be able to implement this in app.module.ts. So, I tried creating a separate provider extending socket. But there also you have to do it in the constructor(), where I am not able to do anything else such as getting the id from another provider since I have to use super() and can't use this..

What are my options?

gaurav112 commented 6 years ago

Do you have a solution ?? I too have this same issue, I want to connet only after login and when my server provides with a token to pass to my socket url

murshid1988 commented 6 years ago

@gaurav112

Yes, basically when the app initializing I check if the token in my storage is valid with the server, if it threw an error I run socket.disconnect();. If I got a success response I run socket.connect(); Also in the login method, I received a success response only I run socket.connect();. Else socket.disconnect();

Also, I do socket.disconnect() when he logs out. In the server, I maintain a pending message feature associated with the ID. So when the particular person comes online I deliver the messages that were sent to him when he was offline.