laravel icon indicating copy to clipboard operation
laravel copied to clipboard

Broadcasting Event with Electron IPC System not working

Open mho22 opened this issue 1 year ago • 4 comments

What happened?

Two things don't actually work or exist from the NativePHP documentation.

  1. There's no ipcRenderer exported from @electron/remote package. There is only a ipcMain Event Emitter.

-> This could be replaced in the documentation by

import * as remote from 'electron';


remote.ipcMain.on( 'native-php', ( _, data ) => ... );
  1. It seems Vite externalizes all Node modules from browser compatibility. But actually, electron uses it in the node_moidules/electron/index.js` file.
Module "path" has been externalized for browser compatibility. Cannot access "path.join" in client code. 
See http://vitejs.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.
Uncaught TypeError: path.join is not a function
    at node_modules/electron/index.js (index.js:4:23)
    at __require (chunk-UXIASGQL.js?v=7bde630a:8:50)
    at electron.js?v=7bde630a:5:16

How to reproduce the bug

  1. Install dependency :

npm install @electron/remote

  1. Add in NativeAppServiceProvider :
Menu::new()->appMenu()->subMenu( 'Events', Menu::new()->label( 'Send Event' ) )->register();
  1. Add in Vue Component :
import * as remote from '@electron/remote';

remote.ipcMain.on( 'native-php', () => console.log( 'event listened' ) );
  1. Build
npm run dev

php artisan native:serve

Package Version

0.4.0

PHP Version

8.1.21

Laravel Version

10.18.0

Node Version

18.10.0

Which operating systems does with happen with?

macOS

Notes

I tried to correct the error with vite-plugin-node-polyfills. No more warning but fs was null.

mho22 avatar Aug 10 '23 13:08 mho22

Can't reproduce since I can't serve my app in 0.4 version but this how I log every events. Hope it'll help somebody :)

window.addEventListener('livewire:load', () => {
    // Sometimes not handled by Native with Livewire 🤷‍
    window.Livewire.addHeaders({
        'X-NativePHP-Secret': '{{ config('nativephp-internal.secret') }}',
    })

    window.Echo.channel('nativephp')
        .subscribed(() => console.info('Subscribed to nativephp channel'))
        .error((error) => console.error(error))
        // Not in Echo public API but works 😉
        .listenToAll((event, payload) => {
            console.log(event, payload)
            // See https://github.com/NativePHP/electron-plugin/blob/main/src/preload/index.ts
            //window.Livewire.emit(event, payload)
        })

    /** @type {Electron} */
    const remote = window.remote;
    // Helps me to keeping track of the window load
    remote.shell.beep()

    remote.ipcMain.on('native-event', (_, data) => {
        console.log(data);
    });
})

MarceauKa avatar Aug 10 '23 15:08 MarceauKa

Can't reproduce since I can't serve my app in 0.4 version but this how I log every events. Hope it'll help somebody :)

window.addEventListener('livewire:load', () => {
    // Sometimes not handled by Native with Livewire 🤷‍
    window.Livewire.addHeaders({
        'X-NativePHP-Secret': '{{ config('nativephp-internal.secret') }}',
    })

    window.Echo.channel('nativephp')
        .subscribed(() => console.info('Subscribed to nativephp channel'))
        .error((error) => console.error(error))
        // Not in Echo public API but works 😉
        .listenToAll((event, payload) => {
            console.log(event, payload)
            // See https://github.com/NativePHP/electron-plugin/blob/main/src/preload/index.ts
            //window.Livewire.emit(event, payload)
        })

    /** @type {Electron} */
    const remote = window.remote;
    // Helps me to keeping track of the window load
    remote.shell.beep()

    remote.ipcMain.on('native-event', (_, data) => {
        console.log(data);
    });
})

Hello, Can you help me?

I want to listen from event from livewire, But it's not working. Can you help me on this?

Nuhel avatar Aug 14 '23 17:08 Nuhel

@mho22 I know this was from a while ago, but can you give some context around what you were trying to achieve?

simonhamp avatar May 14 '24 07:05 simonhamp

@simonhamp As I remember, I wanted to catch events from clicking on a subMenu. I remember trying out what is written in this part of the documentation but it didn't work for me.

I think it was related to the DOOM Native PHP side project : I think I was trying to load different games from the menubar.

I can see that the documentation hasn't changed. But I don't know if the issue is outdated or not. I can try to reproduce the issue again in the next days if you want.

mho22 avatar May 14 '24 09:05 mho22

Fixed by https://github.com/NativePHP/laravel/pull/367 @simonhamp?

gwleuverink avatar Sep 13 '24 12:09 gwleuverink

I think it will be. Thanks for digging through related issues @gwleuverink 😇

@mho22 @MarceauKa @Nuhel the PR that Willem linked will be released next week. Please try that out at your leisure. In the meantime, I'm going to close this issue

simonhamp avatar Sep 13 '24 12:09 simonhamp