'Loading saved messages using socket shows duplicates to other users

So basically I have a chat app in html and nodejs and js and I am trying to load messages that were sent previously so people can see chat history if they join but when they join it works but if say I reload the page on my computer while they are on the site it would send duplicate value when I want it to only show once.

let msgArray = []

const updateCounter2 = ctx => {
  ctx.io.emit('count', Object.keys(ctx.io.sockets.sockets).length);
  for (var i in msgArray) {
 ctx.io.emit('message', msgArray[i]);
}
};

server([get('/', ctx => render('msg.hbs')),
  socket('connect', updateCounter2),
  socket('message', ctx => {
    console.log(ctx.data);
    ctx.io.emit('message', ctx.data);
    msgArray.push(ctx.data)
  })
]);

is part of my code but this is the code to get messages and push them to an array to display when a user loads the page.

Like if I reload the page it works fine but if I reload once someone is on it they get sent the data once again which is not supposed to happen.

Like I am probably repeating myself but I open the page on my phone and on my computer and when I have it open on computer and phone and reload on phone it sends duplicates to computer. (but then if I reload it removes the duplicates and only sends the non dupes)



Solution 1:[1]

Sending clientside message worked

ctx.socket.emit('message', msgArray[i]);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tyler2P