I'm building a one to one chat with Socket.io and RabbitMQ
My implementation of doing a one to one chat is working, but I'm not sure whether the implementation is correct or not.
roomIDis equivalent to for example42314- the
roomIDis autogenerated when user matched with another user. - The logic is simple, user A join a room
42314and it will emit to the server. Once user A joined the room, he can send a message which emits the message from the client to the server.
rabbit.connect("amqp://localhost", (connError, connection) => { connection.createChannel((channelError, channel) => { io.on("connection", (socket) => { // This is where user join the room // and the channel add a new queue which the room ID socket.on("join", (room) => { channel.assertQueue(room.roomID, { durable: false, }); socket.join(room.roomID); }); // This is the part where user send message to another user socket.on("sendMessage", (room) => { // Once user send a message, it will send to the queue. channel.sendToQueue(room.roomID, Buffer.from(room.message)); // since the queue has the room id, whenever there is a message // emit it to the room. channel.consume( room.roomID, (msg) => { console.log(" Received " + msg.content.toString()); io.in(room.roomID).emit("message", msg.content.toString()); }, { noAck: true, } ); }); }); }); });
My questions are
- Does my implementation correct?
- Can this architecture work if there is a load balancer?