Let's deep dive into KOmegle
Komegle is a free online chat website that allows you to chat with strangers. The concept is similar to Omegle.
Built with curiosity🤔 using Node.js and Socket.io
- UserMe hops into chat
- He gets added into a room
- Waits for other users to join
- When UserYou joins, UserMe gets a notification
- UserYou joins into UserMe's room
- In that room they can interact with each other
- UserMe hops into chat
- He gets added to a Socket.IO room. Socket.IO room is assigned to him based on his socket ID.
In KOmege, Socket.IO room is just an array. When a room has no people, UserMe's ID is room ID and put into the array.
-
When UserYou joins, UserMe gets a notification. UserYou gets added to the same room as UserMe with the room ID of UserMe's socket ID.
-
When UserYou joins, Socket.IO emits a message to the room he's in. This way UserMe gets a notification.
-
UserMe and UserYou can interact with each other in the same room. Whenever someone sends message, it gets emitted to the room and everyone in the room gets the message.
-
When UserHe hops into chat, users.js goes through the array.
- If an array has two elements:
- He skips the array and searches for another array.
- If an array has one element:
- He adds UserHe's socket ID to the array and assigns the array as UserHe's room ID.
- If he can't find any array with one element:
- He creates a new array with UserHe's socket ID and assigns the array as UserHe's room ID. Thus creating new room and adding UserHe to it.
- If an array has two elements:
-
When someone leaves the room, Socket.IO emits a message to the room he's in. This way UserWhoever is in room gets a notification.