title: send previous messages in group chat
body: <p>I work at a chat application. I want to store messages in an object like this :</p>
  <pre><code>{
    'room1': ['msg1', 'msg2', ...]
    'room2': ['msg3', 'msg4', ...]
    ...
  }
  </code></pre>
  <p>I defined a variable in my socket.io server <code>roomMessages</code>
  the problem is when I want to add a message in a specific room using <code>roomMessages[room].push(data);</code> it shows me this error: <code>TypeError: Cannot read properties of undefined (reading 'push')</code>
  this is my socket.io code:</p>
  <pre><code>const roomMessages = {};
  
  roomsNamespace.use((socket, next) =&gt; {
    const token = socket.handshake.query.token;
    console.log(token);
    if (!token) return next(new Error(&quot;not auth&quot;));
    next();
  });
  
  roomsNamespace.on(&quot;connection&quot;, (socket) =&gt; {
    const token = socket.handshake.query.token;
    const apiService = new ApiService(process.env.BE_URL, token);
    const room = socket.handshake.query.room;
    socket.on(&quot;join-room&quot;, async () =&gt; {
      socket.join(room);
      await apiService.joinRoom(room);
      const usersInThisRoom = await apiService.getUsersInRoom(room);
      roomsNamespace.in(room).emit(&quot;all-users&quot;, usersInThisRoom);
      socket.emit(&quot;room-messages&quot;, roomMessages[room]);
    });
    socket.on(&quot;chat-message&quot;, (data) =&gt; {
      // console.log(data);
      if (roomMessages[room]) roomMessages[room].push(data);
      roomsNamespace.in(room).emit(&quot;chat-message&quot;, data);
    });
  </code></pre>
  <p>basically, I want to send previous messages when user joins the chat
  How to fix this error?</p>