안정적으로 연결 성공. 이제 로컬에서 테스트함.

정리 - api에 요청 데이터가 들어오면 서버에서 MQ를 사용해서 요청 데이터 바디의 일부분을 클라이언트의 MQ 클라이언트에게 보내야함.

요약하면

  1. api 요청으로 배달 채팅방을 만들면 채팅방 id를 리턴함.
  2. 클라이언트는 채팅방 id를 저장하고 이를 라우팅 키로 사용.
  3. 방에 입장하면 이제 서버와 커넥션을 만들고 키를 가지고 큐에 바인딩을 함.
  4. 요청을 보내면 챡챡 받아와짐.

아래는 서버 코드 정리한 풀리퀘.

Devin by happysubin · Pull Request #1 · Geek-sasaeng/Geek_sasaeng-Server-Chat

아래는 클라이언트 코드 정리한 파일.

const amqp = require('amqplib');

async function main(){
    const queue = 'chatting-room-queue';
    const exchange = "chatting-room-exchange";
    const conn = await amqp.connect('amqp://geeksasaeng:rlrtktod!123@localhost',{
        username : "geeksasaeng",
        password : "rlrtktod!123"
    });

    const ch1 = await conn.createChannel();
    const ho = await ch1.assertExchange(exchange, 'topic', {
        durable: false
      });
    console.log(ho);
    const result = await ch1.assertQueue(queue);
    console.log(result)
    const result2 = await ch1.bindQueue(queue, exchange, "chatting.room.6336dc04e31f053ba1878d69");
    console.log(result2);

    // Listener
    ch1.consume(queue, (msg) => {
        console.log(msg)
        if (msg !== null) {
            console.log('Recieved:', msg.content.toString());
            ch1.ack(msg);
        } else {
            console.log('Consumer cancelled by server');
        }
    });

}

main();

//https://www.npmjs.com/package/amqplib

참고로 JWT랑 기본적인 긱사생 서버 설정도 넣어버림. 스웨거 이런거.

요청 값은 아래와 같음.