1. Server Setup
The IPC server is what runs on your bot’s process. It will run within the same loop as your bot without interfering with your bot’s process.
The server handles multiple things:
- Routes
These routes / endpoints are available to the client and are what your server returns upon requests being made to it.
- Authentication
The IPC client and server use a secret key authentication system. If your server secret key and the request’s authentication header don’t match, the request will not be carried out.
- Multicasting
You do not have to specify a port on your client process, only an IP (defaults to localhost). If you do not specify an IP then the client will connect to another server running on port 20000. This will return the port of your main server for the client to connect to.
So, how does it work? The server is simply just a websocket server. Requests are sent in a JSON payload to and from the server. For example, a client request could be {‘endpoint’: ‘get_guild_count’, ‘headers’: {…}}. This JSON is processed upon a request being made, and checks for a registered route matching the name of the endpoint supplied. It then calls the method linked to said route and returns the payload to the client.
- nextcord.ext.ipc.server.route(name=None)
Used to register a coroutine as an endpoint when you don’t have access to an instance of
Server
- Parameters
name (str) – The endpoint name. If not provided the method name will be used.
- class nextcord.ext.ipc.server.Server(bot, host='localhost', port=8765, secret_key=None, do_multicast=True, multicast_port=20000)
The IPC server. Usually used on the bot process for receiving requests from the client.
- secret_key
A secret key. Used for authentication and should be the same as your client’s secret key.
- Type
- add_cog(cog, *, override=False)
Hooks into add_cog and allows for easy route finding within classes
- await handle_accept(request)
Handles websocket requests from the client process.
- Parameters
request (
Request
) – The request made by the client, parsed by aiohttp.
- await handle_multicast(request)
Handles multicasting websocket requests from the client.
- Parameters
request (
Request
) – The request made by the client, parsed by aiohttp.
- route(name=None)
Used to register a coroutine as an endpoint when you have access to an instance of
Server
.
- start()
Starts the IPC server.
- update_endpoints()
Ensures endpoints is up to date