Skip to content

Instantly share code, notes, and snippets.

@mahadansar
Created March 31, 2020 01:12
Show Gist options
  • Save mahadansar/e78616c64b7392fb20f75c965027331a to your computer and use it in GitHub Desktop.
Save mahadansar/e78616c64b7392fb20f75c965027331a to your computer and use it in GitHub Desktop.
Pusher - Webhook - Maintain State of Online Channels inside a global variable
// Pusher Logic inside a Web Hook post api to make a list of connections with channels
// online users stored in a global variable
var onlineUsers = [
{ channel: "23", name: "channel_occupied" },
{ channel: "9421321", name: "channel_occupied" },
{ channel: "941", name: "channel_occupied" }
];
// Pusher web hook fires off the event via webhook
// A connection closes -- user goes offline
var event = { channel: "9421321", name: "channel_vacated" };
// new connection -- user comes online
//var event = { channel: "3123", name: "channel_occupied" };
// Our logic to maintain online users variable in api called by webhook
if (event["name"] == "channel_occupied") {
onlineUsers.push(event);
} else if (event["name"] == "channel_vacated") {
onlineUsers = onlineUsers.filter(
item => item["channel"] !== event["channel"]
);
}
console.log(onlineUsers);
@mahadansar
Copy link
Author

Actual Web Hook Post API
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment