You know globals are wrong, don’t you? The prim and proper computer science types
tell us that. But they just make life so much easier! No messing about
passing long parameter lists (or refactoring long parameter lists into a
single object parameter, which then doubles the length of the body code to
many languages they are; in PHP you have to either use the
globals keyword to declare which globals to use, or use the
$_GLOBALS superglobal). When you need to modify them, no
worrying about having to return values or reference parameters. So what were
the good reasons for not using globals? Testing. Yawn. Encapsulation. Side
effects. Blah, blah, blah.
But, in the context of data push applications, there is one situation where globals are going to trip us up: when you need to make two or more connections.
Introducing the Example
I will use a stripped-down SSE example. This code won’t have interesting data, and it won’t have the fallback code for the older browsers. None of that affects the decision of which approach is ...