Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useIpcRenderer.md
1---2category: '@Electron'3---45# useIpcRenderer67Provides [ipcRenderer](https://www.electronjs.org/docs/api/ipc-renderer) and all of its APIs with Vue reactivity.89## Usage1011```ts12import { useIpcRenderer } from '@vueuse/electron'13import { computed } from 'vue'1415// enable nodeIntegration if you don't provide ipcRenderer explicitly16// see: https://www.electronjs.org/docs/api/webview-tag#nodeintegration17const ipcRenderer = useIpcRenderer()1819// Ref result will return20const result = ipcRenderer.invoke<string>('custom-channel', 'some data')21const msg = computed(() => result.value?.msg)2223// remove listener automatically on unmounted24ipcRenderer.on('custom-event', (event, ...args) => {25console.log(args)26})27```2829### Available Methods3031| Method | Description |32| ------------------------------------------ | ---------------------------------------------------- |33| `on(channel, listener)` | Listen to channel. Auto-removes listener on unmount. |34| `once(channel, listener)` | Listen to channel once |35| `removeListener(channel, listener)` | Remove specific listener |36| `removeAllListeners(channel)` | Remove all listeners for channel |37| `send(channel, ...args)` | Send async message to main process |38| `invoke(channel, ...args)` | Send message and get response as `ShallowRef` |39| `sendSync(channel, ...args)` | Send sync message and get response as `ShallowRef` |40| `postMessage(channel, message, transfer?)` | Send message with transferable objects |41| `sendTo(webContentsId, channel, ...args)` | Send to specific webContents |42| `sendToHost(channel, ...args)` | Send to webview host |4344### With Custom IpcRenderer4546If `nodeIntegration` is disabled, you can pass the `ipcRenderer` instance explicitly:4748```ts49import { useIpcRenderer } from '@vueuse/electron'50import { ipcRenderer } from 'electron'5152const ipc = useIpcRenderer(ipcRenderer)53```5455## Type Declarations5657```ts58/**59* Result from useIpcRenderer60*61* @see https://www.electronjs.org/docs/api/ipc-renderer62*/63export interface UseIpcRendererReturn {64/**65* Listens to channel, when a new message arrives listener would be called with listener(event, args...).66* [ipcRenderer.removeListener](https://www.electronjs.org/docs/api/ipc-renderer#ipcrendererremovelistenerchannel-listener) automatically on unmounted.67*68* @see https://www.electronjs.org/docs/api/ipc-renderer#ipcrendereronchannel-listener69*/70on: (channel: string, listener: IpcRendererListener) => IpcRenderer71/**72* Adds a one time listener function for the event. This listener is invoked only the next time a message is sent to channel, after which it is removed.73*74* @see https://www.electronjs.org/docs/api/ipc-renderer#ipcrendereroncechannel-listener75*/76once: (77channel: string,78listener: (event: IpcRendererEvent, ...args: any[]) => void,79) => IpcRenderer80/**81* Removes the specified listener from the listener array for the specified channel.82*83* @see https://www.electronjs.org/docs/api/ipc-renderer#ipcrendererremovelistenerchannel-listener84*/85removeListener: (86channel: string,87listener: (...args: any[]) => void,88) => IpcRenderer89/**90* Removes all listeners, or those of the specified channel.91*92* @see https://www.electronjs.org/docs/api/ipc-renderer#ipcrendererremovealllistenerschannel93*/94removeAllListeners: (channel: string) => IpcRenderer95/**96* Send an asynchronous message to the main process via channel, along with arguments.97*98* @see https://www.electronjs.org/docs/api/ipc-renderer#ipcrenderersendchannel-args99*/100send: (channel: string, ...args: any[]) => void101/**102* Returns Promise<any> - Resolves with the response from the main process.103* Send a message to the main process via channel and expect a result ~~asynchronously~~.104* As composition-api, it makes asynchronous operations look like synchronous.105*106* @see https://www.electronjs.org/docs/api/ipc-renderer#ipcrendererinvokechannel-args107*/108invoke: <T>(channel: string, ...args: any[]) => ShallowRef<T | null>109/**110* Returns any - The value sent back by the ipcMain handler.111* Send a message to the main process via channel and expect a result synchronously.112*113* @see https://www.electronjs.org/docs/api/ipc-renderer#ipcrenderersendsyncchannel-args114*/115sendSync: <T>(channel: string, ...args: any[]) => ShallowRef<T | null>116/**117* Send a message to the main process, optionally transferring ownership of zero or more MessagePort objects.118*119* @see https://www.electronjs.org/docs/api/ipc-renderer#ipcrendererpostmessagechannel-message-transfer120*/121postMessage: (channel: string, message: any, transfer?: MessagePort[]) => void122/**123* Sends a message to a window with webContentsId via channel.124*125* @see https://www.electronjs.org/docs/api/ipc-renderer#ipcrenderersendtowebcontentsid-channel-args126*/127sendTo: (webContentsId: number, channel: string, ...args: any[]) => void128/**129* Like ipcRenderer.send but the event will be sent to the <webview> element in the host page instead of the main process.130*131* @see https://www.electronjs.org/docs/api/ipc-renderer#ipcrenderersendtohostchannel-args132*/133sendToHost: (channel: string, ...args: any[]) => void134}135/**136* Get the `ipcRenderer` module with all APIs.137*138* @see https://www.electronjs.org/docs/api/ipc-renderer#ipcrenderersendtohostchannel-args139* @see https://vueuse.org/useIpcRenderer140*/141export declare function useIpcRenderer(142ipcRenderer?: IpcRenderer,143): UseIpcRendererReturn144```145