diff --git a/packages/chat/src/index.js b/packages/chat/src/index.js index 95acec9..5dd1326 100644 --- a/packages/chat/src/index.js +++ b/packages/chat/src/index.js @@ -1,5 +1,6 @@ import { channelIsAnonymous, + parseChannel, parseChatChannel, renderChannel, validateChannel, @@ -16,6 +17,7 @@ import Message from './packets/message.server'; import ensureCanonical from './ensure-canonical'; import defaultState, {channelsToHydrate} from './state'; import joinChannel from './join-channel'; +import leaveChannel from './leave-channel'; export * from './state'; @@ -66,6 +68,14 @@ export default { .filter(({type}) => 'r' === type) .filter((channel) => req.userId || channelIsAnonymous(channel)) .map((channel) => joinChannel(latus, channel, socket)); + socket.on('disconnecting', async () => { + Object.keys(socket.rooms).forEach((room) => { + const parsed = parseChannel(room); + if (parsed && 'r' === parsed.type) { + leaveChannel(parsed, socket); + } + }); + }); return Promise.all(joins); }, '@reddichat/state/defaultState': async (req, latus) => ({