title: How to avoid same cookie being written multiple times when multiple window is open
body: <p>I have a simple chat room web app. I want to be able to save chat history into user's cookie using javascript. The problem I just encounter is when user open more than one window, he will be saving chat message multiple times into his cookie. It must be very confusing reading this, please see example below:</p>
  <p>There are only two users A and B in the chat room, A is acting normal but B has two windows open, both in the same chat room page. A says &quot;Good Morning&quot;, B says nothing and what he see is:</p>
  <blockquote>
  <p>A: Good Morning</p>
  </blockquote>
  <p>Nothing is wrong so far, but when B is saving this into his cookie, because he has two windows open, this message will be saved twice into his cookie. So the next time he come to the chat room, the chat history he sees will be:</p>
  <blockquote>
  <p>A: Good Morning</p>
  <p>A: Good Morning</p>
  </blockquote>
  <p>If B opens n windows at the same time, the message is saved n times.</p>
  <p>A solution I have in mind is when saving message into cookie, check if the last message in the cookie is the same as the one being saved, if they are the same, don't save it again.</p>
  <p>But for cases when a user is truly sending same message multiple times, this will omit the duplicated messages which it shouldn't.</p>
  <p>How is this usually solved? I guess users usually can't open two chat room page at the same time but I allow them to do so.</p>