#Need help with IPC Communication Main to Renderer

29 messages · Page 1 of 1 (latest)

odd sky
#

I followed the documentation's code and tried to adapt it to my use case, but I am unable to receive anything on SvelteKit (the framework I use for my frontend)

I was able to confirm that the event HAS been triggered in my preload.ts with a console.log,

Here is my preload.ts

import { contextBridge, ipcRenderer } from "electron";

contextBridge.exposeInMainWorld("electronAPI", {
    onSimStatusUpdate: (callback: any) =>
        ipcRenderer.on("sim-status-update", (value) => callback(value)),
});

and here is my code in my Frontend:

window.electronAPI.onSimStatusUpdate((newStatus: number) => {
            console.log(newStatus)
            connectionStatus.set(newStatus)
        });

I have also confirmed that that line gets executed with a console log, but I never get any log from the newStatus

Im quite lost, and inexperienced with both Electron and IPCs, s I was wondering if anyone could provide me a bit of insight, I probably messed up some syntax somewhere, so feel free to correct me 😄

muted summit
#

First argument you receive is event, not your value

odd sky
muted summit
#

then maybe you are not setting the listener correctly

#

not possible to tell with the code you shared

odd sky
#

I added an extra listener to console log it and it works... I added

ipcRenderer.on("sim-status-update", (_event, value) => console.log(value)) right under it and that gets logged, but the rest is weird and doesnt do anything

odd sky
muted summit
#

I don't know anything about SvelteKit, so I won't be of much help

odd sky
#

Svelte isnt really involved in it, its a basic TS file

export class SimConnectService {
    static initialize() {
        console.log("Initialized")
        window.electronAPI.onSimStatusUpdate((newStatus: number) => {
            console.log(newStatus)
            connectionStatus.set(newStatus)
        });
    }
}

I get "Initialized" in the console, but nnot the new status

muted summit
#

I don't know, everything looks correct to me

odd sky
muted summit
#

check that the listener is correctly set with another console log?

contextBridge.exposeInMainWorld("electronAPI", {
   onSimStatusUpdate: (callback: any) => {
      console.log("here");
      ipcRenderer.on("sim-status-update", (_event, value) => callback(value));
   }
});
odd sky
#

but the moment I try an arrow function to get the value and do something with it it dont work with no error trace

muted summit
#

on you main

#

can you try to return a simple string as value?

#

like "Hello World"

odd sky
#

Wait letme try something, I think I found the issue

#

Nevermind...

odd sky
muted summit
#

hmm, no reason for it to fail of it's just a number

#

I don't know :/

odd sky
#

this crap only happens to me I swear 😂

muted summit
#

oh?

odd sky
#

My trigger was happening before my listeners where registered on my frontend... Added a settimeout to my trigger and it worked

muted summit
#

I really though it was not that because you said you get the log on preload

#

but then preload loads before the renderer so

#

glad you found the issue