Skip to content

Commit

Permalink
show total visitors
Browse files Browse the repository at this point in the history
  • Loading branch information
d-led committed Jun 16, 2024
1 parent 4a7db27 commit 2f546be
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion counter_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ func NewCounterListener(events *pubsub.PubSub[string, Event]) *CounterListener {

func (n *CounterListener) OnNewCount(count int64) {
log.Println("New visitor count:", count)
n.events.Pub(NewEventWithParam("TotalVisitors", count))
n.events.Pub(NewEventWithParam("TotalVisitors", count), Topic)
}
1 change: 1 addition & 0 deletions fly_peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (ps *PeerSource) listenToInternalEventsForever() {
for event := range subscription {
if event.Name == "VisitorJoined" {
ps.counter.Increment()
ps.events.Pub(NewEventWithParam("TotalVisitors", ps.counter.Value()), Topic)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions ui-src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ <h4>Server-side updates & info</h4>
<td>Replicas</td>
<td><span id="replicas"></span></td>
</tr>
<tr class="monospaced">
<td>Total visitors (local for now)</td>
<td><span id="total-visitors"></span></td>
</tr>
</tbody>
</table>
</div>
Expand Down
11 changes: 11 additions & 0 deletions ui-src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ function showReplicasActive(count: number) {
replaceText("#replicas", `${count}`);
}

function showTotalVisitors(count: number) {
if (count == null) {
return;
}
replaceText("#total-visitors", `${count}`);
}

function showServerRevision(text: string) {
replaceText("#server-revision", text);
}
Expand Down Expand Up @@ -172,6 +179,10 @@ async function processEvent(event) {
showReplicasActive(event?.properties?.param);
// do not show this event in the log
return;
case "TotalVisitors":
showTotalVisitors(event?.properties?.param);
// do not show this event in the log
return;
case "Revision":
showServerRevision(event?.properties?.param);
return;
Expand Down

0 comments on commit 2f546be

Please sign in to comment.