# Communication API The following SimConnect functions and enums/structs are used for for communicating data between various SimConnect clients, WebAssembly modules, and JavaScript gauges. You can find information on the other platform communication functions from the following pages: - [JavaScript Communication API](../../../javascript/communication-api/) - [WebAssembly Communication API](../../../wasm/communication-api/communication-api/)   {{< table-wrapper >}} | Function | Description | |----------------------------------------|----------------------------------------------------------------------| | `SimConnect_CallCommBusEvent` | Used to call a communication (CommBus) event. | | `SimConnect_SubscribeToCommBusEvent` | Used to subscribe the client to a communication (CommBus) event | | `SimConnect_UnsubscribeToCommBusEvent` | Used to unsubscribe the client from a communication (CommBus) event. | {{< /table-wrapper >}}   For a list of other SimConnect functions please see the [SimConnect API Reference](../../simconnect-api-reference/).   When using this API it's worth noting the following:   - JSON is a good way to format data for communication between the different platforms. In C++, a version of [RapidJSON](https://rapidjson.org/) is provided as part of the SDK - with some fixes to work in Web Assembly - and can help you when sharing information.   - You can add an ID in function arguments to know the caller, and ensure you are dealing with the correct events.   - All calls are asynchronous.   You can find a sample project to use as a reference when using the Communication API here: - [CommBus](../../../../samples-tutorials/samples/visualstudio/simconnect-samples/#commbus)   There are also debugging tools available in DevMode which can be used to help debug Flow Events and Communication API events, which are described on the following page: - [Debug Platform Dispatcher](../../../../devmode/menus/debug_info/debug-platform-dispatcher/)     ### Calling And Registering Events The communication (CommBus) API is cross platform, and as such, can be used to call *and* register events on the different supported platforms. Below you can find brief overviews for how this can be done. **C++** **C\#** #### Register an event in SimConnect Open a SimConnect connection: ``` cpp HRESULT hr = E_FAIL; HANDLE hSimConnect = NULL; hr = SimConnect_Open(&hSimConnect, "Connection name", NULL, 0, 0, 0); ```   Create a dispatch function: ``` cpp std::string str; void CALLBACK Dispatch(SIMCONNECT_RECV* pData, DWORD cbData, void* pContext) { switch (pData->dwID) { case SIMCONNECT_RECV_ID_COMM_BUS: // The received data are a comm bus event { SIMCONNECT_RECV_COMM_BUS* pCommBusEvt = (SIMCONNECT_RECV_COMM_BUS*)pData; switch (pCommBusEvt->uEventID) // Check which event this is { case EVENT_COMM_BUS: { if (pCommBusEvt->dwOutOf == 1) { PrintReceivedData(pCommBusEvt->rgData); } else { receptionBuffer += data.rgData; if (pCommBusEvt->dwEntryNumber + 1 == pCommBusEvt->dwOutOf) { PrintReceivedData(receptionBuffer); receptionBuffer = ""; } } break; } } } } } ```   Call the dispatch: ``` cpp while( !quit ) { SimConnect_CallDispatch(hSimConnect, Dispatch, NULL); Sleep(1000); } ``` {{< callout context="note" title="NOTE" icon="outline/bulb" >}} In WebAssembly, there is no needs to loop on `SimConnect_CallDispatch`, the given callback will be called each frame. {{< /callout >}}   Register the event: ``` cpp HRESULT hr = E_FAIL; hr = SimConnect_SubscribeToCommBusEvent(hSimConnect, EVENT_COMM_BUS, "SimConnectEventName"); ```     #### {{< glossterm >}}call_js_from_simconnect{{< /glossterm >}} In your JS file you need to create the callback: ``` js myJsCallback(args) { ... } ``` You then need to register the view listener: ``` js this.commBusListener = RegisterCommBusListener(); ``` Next you would connect the callback to an event name: ``` js this.commBuListener.on("MyJsCallback", this.myJsCallback); ``` In the SimConnect client you need to open a SimConnect connection: ``` cpp HRESULT hr = E_FAIL; HANDLE hSimConnect = NULL; hr = SimConnect_Open(&hSimConnect, "Connection name", NULL, 0, 0, 0); ``` Call the event: ``` cpp HRESULT hr = E_FAIL; hr = SimConnect_CallCommBusEvent(hSimConnect, eventName, broadcastTo, strlen(buffer) + 1, buffer); ```   #### {{< glossterm >}}call_wasm_from_simconnect{{< /glossterm >}} Create a WASM callback: ``` wasm static void MyWasmCallback(const char* args, unsigned int size, void* ctx) { ... } ``` Register the event: ``` wasm fsCommBusRegister("MyWasmCallback", MyWasmCallback); ``` Open a SimConnect connection: ``` cpp HRESULT hr = E_FAIL; HANDLE hSimConnect = NULL; hr = SimConnect_Open(&hSimConnect, "Connection name", NULL, 0, 0, 0); ``` Call the WASM event: ``` cpp HRESULT hr = E_FAIL; hr = SimConnect_CallCommBusEvent(hSimConnect, eventName, broadcastTo, strlen(buffer) + 1, buffer); ```   #### {{< glossterm >}}call_simconnect_(c++)_from_simconnect_(c++){{< /glossterm >}} Open a SimConnect connection: ``` cpp HRESULT hr = E_FAIL; HANDLE hSimConnect = NULL; hr = SimConnect_Open(&hSimConnect, "Connection name", NULL, 0, 0, 0); ```   Create a dispatch function: ``` cpp std::string str; void CALLBACK Dispatch(SIMCONNECT_RECV* pData, DWORD cbData, void* pContext) { switch (pData->dwID) { case SIMCONNECT_RECV_ID_COMM_BUS: // The received data are a comm bus event { SIMCONNECT_RECV_COMM_BUS* pCommBusEvt = (SIMCONNECT_RECV_COMM_BUS*)pData; switch (pCommBusEvt->uEventID) // Check which event this is { case EVENT_COMM_BUS: { if (pCommBusEvt->dwOutOf == 1) { PrintReceivedData(pCommBusEvt->rgData); } else { receptionBuffer += data.rgData; if (pCommBusEvt->dwEntryNumber + 1 == pCommBusEvt->dwOutOf) { PrintReceivedData(receptionBuffer); receptionBuffer = ""; } } break; } } } } ```   Call the dispatch: ``` cpp while( !quit ) { SimConnect_CallDispatch(hSimConnect, Dispatch, NULL); Sleep(1000); } ```   Register the SimConnect event: ``` cpp HRESULT hr = E_FAIL; hr = SimConnect_SubscribeToCommBusEvent(hSimConnect, EVENT_COMM_BUS, "SimConnectEventName"); ```   Open a Simconnect connection: ``` cpp HRESULT hr = E_FAIL; HANDLE hSimConnect = NULL; hr = SimConnect_Open(&hSimConnect, "Connection name", NULL, 0, 0, 0); ```   Call the SimConnect event: ``` cpp HRESULT hr = E_FAIL; hr = SimConnect_CallCommBusEvent(hSimConnect, eventName, broadcastTo, strlen(buffer) + 1, buffer); ```   #### {{< glossterm >}}call_simconnect_(c#)_from_simconnect_(c++){{< /glossterm >}} Open a SimConnect (C\#) connection: ``` cs SimConnect m_oSimConnect = new SimConnect("Connection name", m_hWnd, WM_USER_SIMCONNECT, null, 0); ```   Create a CommBus callback: ``` cs string receptionBuffer; void OnCommBusEvent(SimConnect sender, SIMCONNECT_RECV_COMM_BUS data) { switch (data.uEventID) { case ((int)Events.CommBusEvent): { if (data.dwOutOf == 1) { PrintReceivedData(data.rgData); } else { receptionBuffer += data.rgData; if (data.dwEntryNumber + 1 == data.dwOutOf) { PrintReceivedData(receptionBuffer); receptionBuffer = ""; } } } break; default: break; } } ```   Register a callback on Recv CommBus: ``` cs m_oSimConnect.OnRecvCommBus += new SimConnect.RecvCommBusEventHandler(OnCommBusEvent); ```   Register the SimConnect (C\#) event: ``` cs m_oSimConnect.SubscribeToCommBusEvent(Events.CommBusEvent, "SimConnectEventName"); ```   Open a SimConnect (C++) connection: ``` cpp HRESULT hr = E_FAIL; HANDLE hSimConnect = NULL; hr = SimConnect_Open(&hSimConnect, "Connection name", NULL, 0, 0, 0); ```   Call the SimConnect (C++) event: ``` cpp HRESULT hr = E_FAIL; hr = SimConnect_CallCommBusEvent(hSimConnect, "SimConnectEventName", broadcastTo, strlen(buffer) + 1, buffer); ```   #### Register an event in SimConnect Open a SimConnect connection: ``` cs SimConnect m_oSimConnect = new SimConnect("Connection name", m_hWnd, WM_USER_SIMCONNECT, null, 0); ```   Create a CommBus callback: ``` cs string receptionBuffer; void OnCommBusEvent(SimConnect sender, SIMCONNECT_RECV_COMM_BUS data) { switch (data.uEventID) { case ((int)Events.CommBusEvent): { if (data.dwOutOf == 1) { PrintReceivedData(data.rgData); } else { receptionBuffer += data.rgData; if (data.dwEntryNumber + 1 == data.dwOutOf) { PrintReceivedData(receptionBuffer); receptionBuffer = ""; } } } break; default: break; } } ```   Register a callback on Recv CommBus: ``` cs m_oSimConnect.OnRecvCommBus += new SimConnect.RecvCommBusEventHandler(OnCommBusEvent); ```   Register the event: ``` cs m_oSimConnect.SubscribeToCommBusEvent(Events.CommBusEvent, "SimConnectEventName"); ```     #### {{< glossterm >}}call_js_from_simconnect{{< /glossterm >}} In your JS file you need to create the callback: ``` js myJsCallback(args) { ... } ``` You then need to register the view listener: ``` js this.commBusListener = RegisterCommBusListener(); ``` Next you would connect the callback to an event name: ``` js this.commBuListener.on("MyJsCallback", this.myJsCallback); ``` In the SimConnect client you need to open a SimConnect connection: ``` cs SimConnect m_oSimConnect = new SimConnect("Connection name", m_hWnd, WM_USER_SIMCONNECT, null, 0); ``` Call the event: ``` cs String message = "something"; m_oSimConnect.CallCommBusEvent("MyJsCallback", broadcastTo, message); ```   #### {{< glossterm >}}call_wasm_from_simconnect{{< /glossterm >}} Create a WASM callback: ``` wasm static void MyWasmCallback(const char* args, unsigned int size, void* ctx) { ... } ``` Register the event: ``` wasm fsCommBusRegister("MyWasmCallback", MyWasmCallback); ``` Open a SimConnect connection: ``` cs SimConnect m_oSimConnect = new SimConnect("Connection name", m_hWnd, WM_USER_SIMCONNECT, null, 0); ``` Call the WASM event: ``` cs String message = "something"; m_oSimConnect.CallCommBusEvent("MyWasmCallback", broadcastTo, message); ```   #### {{< glossterm >}}call_simconnect_(c++)_from_simconnect_(c#){{< /glossterm >}} Open a SimConnect (C++) connection: ``` cs HRESULT hr = E_FAIL; HANDLE hSimConnect = NULL; hr = SimConnect_Open(&hSimConnect, "Connection name", NULL, 0, 0, 0); ``` Create a dispatch function (C++): ``` cs std::string str; void CALLBACK Dispatch(SIMCONNECT_RECV* pData, DWORD cbData, void* pContext) { switch (pData->dwID) { case SIMCONNECT_RECV_ID_COMM_BUS: // The received data are a comm bus event { SIMCONNECT_RECV_COMM_BUS* pCommBusEvt = (SIMCONNECT_RECV_COMM_BUS*)pData; switch (pCommBusEvt->uEventID) // Check which event this is { case EVENT_COMM_BUS: { if (pCommBusEvt->dwOutOf == 1) { PrintReceivedData(pCommBusEvt->rgData); } else { receptionBuffer += data.rgData; if (pCommBusEvt->dwEntryNumber + 1 == pCommBusEvt->dwOutOf) { PrintReceivedData(receptionBuffer); receptionBuffer = ""; } } break; } } } } ``` Call the dispatch (C++): ``` cs while( !quit ) { SimConnect_CallDispatch(hSimConnect, Dispatch, NULL); Sleep(1000); } ``` Register the SimConnect (C++) event: ``` cs HRESULT hr = E_FAIL; hr = SimConnect_SubscribeToCommBusEvent(hSimConnect, EVENT_COMM_BUS, "SimConnectEventName"); ``` Open a SimConnect (C\#) connection: ``` cs SimConnect m_oSimConnect = new SimConnect("Connection name", m_hWnd, WM_USER_SIMCONNECT, null, 0); ``` Call the SimConnect (C\#) event: ``` cs String message = "truc"; m_oSimConnect.CallCommBusEvent("SimConnectEventName", broadcastTo, message); ```   #### {{< glossterm >}}call_simconnect_(c#)_from_simconnect_(c#){{< /glossterm >}} Open a SimConnect connection: ``` cs SimConnect m_oSimConnect = new SimConnect("Connection name", m_hWnd, WM_USER_SIMCONNECT, null, 0); ``` Create a CommBus callback: ``` cs string receptionBuffer; void OnCommBusEvent(SimConnect sender, SIMCONNECT_RECV_COMM_BUS data) { switch (data.uEventID) { case ((int)Events.CommBusEvent): { if (data.dwOutOf == 1) { PrintReceivedData(data.rgData); } else { receptionBuffer += data.rgData; if (data.dwEntryNumber + 1 == data.dwOutOf) { PrintReceivedData(receptionBuffer); receptionBuffer = ""; } } } break; default: break; } } ``` Register a callback on Recv CommBus: ``` cs m_oSimConnect.OnRecvCommBus += new SimConnect.RecvCommBusEventHandler(OnCommBusEvent); ``` Register the SimConnect event: ``` cs m_oSimConnect.SubscribeToCommBusEvent(Events.CommBusEvent, "SimConnectEventName"); ``` Open a SimConnect connection: ``` cs SimConnect m_oSimConnect = new SimConnect("Connection name", m_hWnd, WM_USER_SIMCONNECT, null, 0); ``` Call the SimConnect event: ``` cs String message = "something"; m_oSimConnect.CallCommBusEvent("SimConnectEventName", broadcastTo, message); ```