Multiplayer Bot Architecture for Phoenix App?

I have a multiplayer web game that uses Phoenix Channels for communication with clients via websockets. I’d like to add server-side bot players to my game.

I’ve given some thought on where in the stack to add the bots. My current plan is to simulate the bots at the “edge” of the system as GenServers that are subscribed to the same phoenix endpoint channel that actual clients are. It is my understanding that they will need to maintain virtual Socket connections to accomplish this.

I’ve been trying to figure out how to explicitly create sockets and subscribe them to a Phoenix Channel. I’ve looked at the code for channel testing utilities as a starting point.

Alternatively, I was considering implementing an intermediate “event server” sitting between the game server and the actual endpoint channel. This event server would be a pass through for all game chit chat and would allow me to handle event delegation to bots and actual clients discriminately. In this case, a bot would still be a GenServer with event behaviors but it would not need to simulate an actual socket. Curious to hear thoughts.

I don’t think that creatng an imulation for socket is a good idea. It looks like bot even doesn’t belong to web application. I would place it somewhere in OTP app, it’s hard to tell exact where, since i don’t know how you handle every player, but if through gen servers, Bot.Server could be near Player.Server

1 Like

I will go with second approach, although we can avoid that event server, and game-server can actually handle event delegation to bots/players. Bots will be genservers and will be registered with the game-server, and when bots have to send any message to phoenix channel, they can do that by MyAppWeb.Endpoint.broadcast!(channel_name, "bot_msg", %{"msg" => "attack"}). Channel name can be stored in the game-server.

1 Like

I think more clear way to do that is to send messages directly to game server, inside OTP. And game will send it on a regular basis to players(as does on real player events)

1 Like

Yeah, I’m leaning this way too now. I currently do MyAppWeb.Endpoint.broadcast... from my game server to push events to clients, so I could just add an extra function call to “broadcast” the event to bots as well. Probably don’t even need a middleman event server to your point. Was thinking adding an event server would improve modularity/testability but probably is more complexity than its worth.

Yeah, From game-server we can broadcast like usual to the channel.