As @hardillb said you are comparing references so they will never be equal. You will have to compare properties.
If you are just looking for a change in PRES.presence
you could use;
if (PRES.presence != PRESAnt.presence)
If you wish to compare many properties you will have to use an Object comparison function, so;
if (isOjectEqual(PRES, PRESAnt))
/**
* Description Deep compare of two objects.
*
* @param {*} x Any object that has Object as prototype i.e. Object, Array, Date, String, Map, Set, Number, etc
* @param {*} y Any object that has Object as prototype i.e. Object, Array, Date, String, Map, Set, Number, etc
*
* @returns {boolean} Whether the two objects are equivalent, that is, every property in x is equal to every property in y recursively. Primitives
* must be strictly equal, that is "1" and 1, null and undefined and similar objects are considered different
*/
function isObjectEqual(x, y) {
let seen = [];
return (function equals(x, y) {
// If both x and y are null or undefined and exactly the same
if (x === y) {
return true;
}
// If they are not strictly equal, they both need to be Objects
if (!(x instanceof Object) || !(y instanceof Object)) {
return false;
}
// They must have the exact same prototype chain, the closest we can do is test the constructor. if (x.constructor !== y.constructor) {
return false;
}
for (const p in x) {
// Inherited properties were tested using x.constructor === y.constructor
if (x.hasOwnProperty(p)) {
// Allows comparing x[p] and y[p] when set to undefined
if (!y.hasOwnProperty(p)) {
return false;
}
// If they have the same strict value or identity then they are equal
if (x[p] === y[p]) {
continue;
}
// Numbers, Strings, Functions, Booleans must be strictly equal
if (typeof(x[p]) !== "object") {
return false;
}
// Test cyclicality
if (seen.indexOf(x[p]) !== -1) {
throw new Error("Cannot compare some cyclical objects");
}
seen.push(x[p]);
// Objects and Arrays must be tested recursively
if (!equals(x[p], y[p])) {
return false;
}
}
}
for (const p in y) {
// allows x[p] to be set to undefined
if (y.hasOwnProperty(p) && !x.hasOwnProperty(p)) {
return false;
}
}
return true;
})(x, y);
} // End Function isObjectEqual()
This is a function I found on t'internet and it seems to work but I am sure there are others. If you are wanting to send a different message when any property in the two objects change you will have to compare each property, probably using Object.entries
PS If you want msg#.payload
to hold the 'Changed' message you can also use
let msg2.payload = "Nothing changed"