Last active
August 29, 2015 14:27
-
-
Save scalabl3/f2f64038618fb8f5851c to your computer and use it in GitHub Desktop.
Normalizing Callback returns on Subscribe and Presence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function normalize_subscribe_message_callback_object(msg, envelope) { | |
var result = { | |
channel_group: null, | |
channel: null, | |
message: msg | |
}; | |
// if the message received through channel group | |
if (envelope.length === 4) { | |
result.channel_group = envelope[2]; | |
result.channel = envelope[3]; | |
} | |
// if message received through channel only | |
else if (envelope.length === 3) { | |
result.channel = envelope[2]; | |
} | |
else { | |
console.error("Subscribe Message Callback envelope parameter array length NOT IN [3,4]") | |
} | |
return result; | |
} | |
function normalize_presence_message_callback_object(msg, envelope) { | |
var result = { | |
channel_group: null, | |
channel: null, | |
message: msg | |
}; | |
// if the message received through channel group | |
if (envelope.length === 4) { | |
result.channel_group = envelope[2].slice(0, envelope[2].length - 7); | |
result.channel = envelope[3].slice(0, envelope[3].length - 7); | |
} | |
// if message received through channel only | |
else if (envelope.length === 3) { | |
result.channel = envelope[2].slice(0, envelope[2].length - 7); | |
} | |
else { | |
console.error("Presence Message Callback envelope parameter array length NOT IN [3,4]") | |
} | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
p.subscribe({ | |
channel_group: "my_channel_group", | |
message: function(msg, env, a, b) { | |
// You can remove this log statement | |
console.log("\tMESSAGE: ", msg, env, a, b); | |
var result = normalize_subscribe_message_callback_object(msg, env); | |
// Do Something with normalized result object | |
// ... | |
}, | |
presence: function(msg, env, a, b) { | |
// You can remove this log statement | |
console.log("\tPRESENCE: ", msg, env, a, b); | |
var result = normalize_presence_message_callback_object(msg, env); | |
// Do Something with normalized result object | |
// ... | |
}, | |
connect: function() { | |
console.log("\tCONNECTED: ", chan); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
p.subscribe({ | |
channel: "my_channel", | |
message: function(msg, env, a, b) { | |
// You can remove this log statement | |
console.log("\tMESSAGE: ", msg, env, a, b); | |
var result = normalize_subscribe_message_callback_object(msg, env); | |
// Do Something with normalized result object | |
// ... | |
}, | |
presence: function(msg, env, a, b) { | |
// You can remove this log statement | |
console.log("\tPRESENCE: ", msg, env, a, b); | |
var result = normalize_presence_message_callback_object(msg, env); | |
// Do Something with normalized result object | |
// ... | |
}, | |
connect: function() { | |
console.log("\tCONNECTED: ", chan); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment