1.0.1 • Published 1 year ago

@yealink_dev/yealink-ipc-helper-rcc v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Yealink Node.js Electron Renderer Helper

This optional package allows a secure render process in ElectronJs (https://electronjs.org/) to transparently access the full Yealink Node.js SDK API through proxies implemented on top of Electron's IPC mechanism. This makes it much easier for a sandboxed render process to call into Yealink classes without having to pass messages around.

Note that this package is optional and only potentially useful in a sandboxed electron setup, where the render process does not have access rights to call into the node modules such as the Yealink Node.js API npm module. Even in this case this package is optional, as electron applications can perfectly decide to manage any such messaging themselves.

Usage tips

  1. Get a Yealink API Key from yealink teams

  2. Install dependencies to your existing electron project

    npm install yealink-node-sdk
    npm install yealink-ipc-helper
  3. Configure your existing MAIN process script

    Add a top level imports and global:

    import { app, BrowserWindow, ipcMain } from 'electron';
    import { ConfigParamsCloud } from '@yealink_dev/yealink-node-sdk-rcc';
    import { YLIPCServerFactory, YLIPCServer } from '@yealink_dev/yealink-ipc-helper-rcc';
    
    let ylIPCServerFactory: YLIPCServerFactory | null = null;
    let ylIPCServer: YLIPCServer | null = null;

    Add a preload script to your BrowserWindow instantiation if it is missing:

    preload: path.join(__dirname, 'preload.js')

    Setup yealink api server when electron is ready:

    app.on("ready", async () => {
      // Setup the yealink server factory BEFORE creating GUI.
      ylIPCServerFactory = new YLIPCServerFactory(ipcMain);
    
      // Code to create GUI window here
      // Tip: Return window.loadFile promise or wait for 'did-finish-load' event and convert it to a promise.
      let fullyLoadedWindow = await your_code_to_create_gui_that_resolves_when_loaded();
    
      // As window is now fully loaded we can instantiate our api server for the client.
      ylIPCServer = await ylIPCServerFactory.create('<Set your Yealink API key here>', 
                                                    { /* config here if needed */}, 
                                                    false,
                                                    fullyLoadedWindow);
    });

    Cleanup when closing:

    app.on("window-all-closed", async () => {
        if (process.platform !== "darwin") {
            if (ylIPCServer) {
                await ylIPCServer.shutdown();
                ylIPCServer = null;
            }
            if (app) {
                app.quit();
            }
        }
    });
  4. Configure the preload script to expose ipcRender to the renderer process

    import { ipcRenderer } from 'electron';
    
    window.electron = { 
        ipcRenderer
    };
  5. Configure to your render javascript (loaded by your html)

    import { createIPCClient } from '@yealink_dev/yealink-ipc-helper-rcc';
    import { DeviceImpl, YLSdkImpl, ylEventsList, DeviceEventsList } from '@yealink_dev/yealink-node-sdk-rcc';
    
    createIPCClient(window.electron.ipcRenderer).then((client) => {    
        client.on('attach', (device) => {
            // TODO: Add code here to call into provided DeviceImpl proxy. 
        });
    
        client.on('detach', (device) => {
        // TODO: Add code here if needed.
        });
    }).catch( (err) => {
        console.error("Could not initialize Yealink Api client : " + err);
    });
  1. Use typescript along with browserify, webpack or other build tool to produce a javascript bundle for the renderer.

Complete example and details.

Refer to the democallcontrol for a complete example of how to use this package.