Websocket alerts based on the Observer Design Pattern


#1

Greetings,

It is my understanding that there exists a Websocket API (Web Panel WebSocket API) which allows for access to the phantombot database as if one was making CGI requests against the same database (request-response model) and there also exists the addons file API ([Stream Labels - Console - FAQ - German / English) which allows for modules to create their own files for others to request. Neither of these aren’t sufficient for what I want (polling for data)

It would be nice if scripts could connect (and authenticate) to the websocket API, then register for events from particular modules. The modules would also need a way to send events. This would allow for events (such as a new follow) to get noticed by the follow handler, then forward it to those who are interested in follows. In my case, this would be used to power custom alerts and overlays and only phantom bot would be bothering twitch (or other services) with that information.

Here is the request flow that I see:

Client A:
Sends subscription request to phantombot for ./handlers/followHandler.js

*phantombot adds Client A's socket to the list of observers for ./handlers/followHandler.js*

Client B:
Sends subscription request to phantombot for ./handlers/followHandler.js and ./handlers/subscribeHandler.js

*phantombot adds Client B's socket to the list of observers for ./handlers/followHandler.js and ./handlers/subscribeHandler.js*

*Twitch User John667 follows streamer*
*./handlers/followHandler.js notices and tells phantombot to emit a message*
*Phantombot sends follow notification to Client A and B (without them requesting it besides their initial subscription)*

*Twitch User John667 subscribes to streamer*
*./handlers/subscribeHandler.js notices and tells phantombot to emit a message*
*Phantombot sends sub notification to Client B (again, without them requesting it. Server pushes data to client)*

As far as what clients see, if it doesn’t follow the socket.io standard, I would expect something in a similar format to below:

{
  "module": "./handlers/followHandler.js",
  "data": [{
    "name": "John667",
    "description": "More fields defined by the follower handler including info as to what is important",
    "...": "..."
  },{
    "name": "second follower in same poll period?",
    "...": "..."
  }]
}

I realize that this is a request (and I’m probably being way too specific for a request), but this is the kind of thing that I’m looking for and want to do. I want to create my own custom alerts and do things my own way, gradually moving away from streamlabs/streamelements and moving towards using phantombot and my own tools. The above change should allow for a lot more flexibility when it comes to other apps integrating with Phantombot.