Answer the question
In order to leave comments, you need to log in
How to get web socket connection status in Vue component?
The socket connection is set up in index.js like this:
Vue.use(new VueSocketIO({
debug: true,
connection: SocketIO(process.env.BACKEND_URL, {query: `auth_token=${Vue.auth.token()}`}),
vuex: {
store,
actionPrefix: 'SOCKET_',
mutationPrefix: 'SOCKET_'
},
}));
sockets: {
connect: function() {
this.connect = true
}
},
<div id="app" v-if="appReady">
<router-view />
</div>
appReady() {
return this.authReady && this.connect
},
io.use(jwtAuth.authenticate({
secret: process.env.JWT_SECRET,
algorithm: 'HS256',
succeedWithoutToken: true
}, async (payload, done) => {
if(payload && payload._id) {
try {
const user = await (await User.findById(payload._id)).toObject();
if(!user) return done(null, false, 'user does not exist');
user._id = user._id.toString();
done(null, user);
} catch (error) {
done(error);
}
} else {
return done();
}
}));
io.on('connection', (client) => {
if (clients[client.request.user._id]) {
return;
}
clients[client.request.user._id] = {
clientId: client.id,
role: client.request.user.role,
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question