Created
March 31, 2020 01:12
-
-
Save mahadansar/e78616c64b7392fb20f75c965027331a to your computer and use it in GitHub Desktop.
Pusher - Webhook - Maintain State of Online Channels inside a global variable
This file contains hidden or 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
// 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actual Web Hook Post API
