It seems that socket.io is converting a set into an empty object when sending from a node js back end to the front end. Why would this be? Is it a limitation of JSON, bug in socket, or bug in node js?
On the front end I'm just doing something like socket.on('room', (data) => console.log(data));
In my node server I've tried this:
const set = new Set;
set.add("Test");
console.log(set); // outputs as: Set { 'Test' }
mySocketIoNameSpace.emit('room', set); // ends up on the front end as an empty object {}
...which doesn't work. Whereas this works fine:
const arr = new Array();
arr.push("Test");
mySocketIoNameSpace.emit('room', arr); // front end gets: ["Test"]
const obj = new Object();
obj["Test"] = true;
mySocketIoNameSpace.emit('room', obj); // front end gets: {Test: true}
I figure I will have to emit something like Array.from(set)
, or just use an array or object rather than a set, which seems unfortunate since it would detract from the set's speed or the uniqueness.