{"id":18256143,"url":"https://github.com/serapath/electron-websocket-stream","last_synced_at":"2025-04-04T17:31:53.013Z","repository":{"id":57222016,"uuid":"79664027","full_name":"serapath/electron-websocket-stream","owner":"serapath","description":"spawn browser windows connected to the main process via websocket-stream ","archived":false,"fork":false,"pushed_at":"2017-01-29T20:16:03.000Z","size":23,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-06T21:46:41.342Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/electron-websocket-stream","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/serapath.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-21T18:22:06.000Z","updated_at":"2020-02-24T16:15:59.000Z","dependencies_parsed_at":"2022-09-04T06:50:48.305Z","dependency_job_id":null,"html_url":"https://github.com/serapath/electron-websocket-stream","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serapath%2Felectron-websocket-stream","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serapath%2Felectron-websocket-stream/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serapath%2Felectron-websocket-stream/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serapath%2Felectron-websocket-stream/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/serapath","download_url":"https://codeload.github.com/serapath/electron-websocket-stream/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223151374,"owners_count":17096098,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-05T10:20:10.345Z","updated_at":"2024-11-05T10:20:11.094Z","avatar_url":"https://github.com/serapath.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# electron-websocket-stream\nspawn browser windows connected to the main process via websocket-stream\n\n# usage\n`npm install electron-websocket-stream`\n```js\nvar electrows = require('electron-websocket-stream')\n\nvar options = { debug: false }\n// `debug` logs some connection details and controls whether errors\n// in the browser are sent to the http server for logging\n\nelectrows(options, callback)\n\n/********************************************************************\n  MAIN PROCESS\n\n `electron.BrowserifyWindow` is additionally supported and\n returns a \"BrowserWindow instance\" that supports the\n additional method: `.webContents.connectFunctionScript` with one\n parameter `scriptFn`.\n\n Calling `.webContents.connectFunctionScript(myScriptFn)` requires\n `myScriptFn` to have it's first parameter named `ws` and\n returns a `websocket-stream` instance.\n   1. The returned `ws` instance lives in the MAIN PROCESS.\n   2. The argument passed to the `ws` parameter of `myScriptFn`\n      is the `websocket-stream` instance that lives in the\n      RENDER PROCESS.\n   3. This constitutes a real time stream connection\n      that allows communication between the main and render process\n********************************************************************/\nfunction callback (error, electron) {\n  if (error) throw error\n\n  var BrowserifyWindow = electron.BrowserifyWindow\n\n  var opts = { width: 800, height: 600, show: true }\n  var win = BrowserifyWindow(opts)\n  win.loadURL('http://www.google.de')\n  // win.openDevTools()\n  var ws = win.webContents.connectFunctionScript(scriptFn)\n\n  // listen to the 'data' event\n  ws.on('data', function (data) {\n    console.log('receive data in main process:')\n    console.log(typeof data)\n    console.log (data)\n  })\n  ws.on('end', function () { console.log('end') })\n  ws.write('DATA2')\n  // ws.on('finish', function () {console.log('finish')})\n  // ws.on('close', function () {console.log('close')})\n  // ws.on('exit', function () {console.log('exit')})\n  // or pipe to another stream\n  // ws.pipe(...)\n\n  return ws // [optional] return a duplex stream\n}\n/********************************************************************\n  RENDER PROCESS\n\n  * `scriptFn` will be stringified and browserified to run in a BrowserWindow\n  * `ws` parameter will be passed a websocket-stream instance arguments\n       for communication with the main process\n  * ending `ws` exits the browser window\n********************************************************************/\nfunction scriptFn (ws) {\n\n  var bel = require('bel')\n\n  var element = bel`\u003ch1\u003e hello world \u003c/h1\u003e`\n  document.body.innerHTML = ''\n  document.body.appendChild(element)\n\n  var data = {\n    title    : document.title,\n    url      : location.href,\n    content  : document.querySelector('h1').innerText\n  }\n\n  ws.on('data', function (data) {\n    console.log('receive data in browserify window:')\n    console.log(data)\n  })\n  ws.write(data) // sende data to the main process\n\n  // ws.end() // closes the websocket stream \u0026 exits the BrowserifyWindow `win`\n\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserapath%2Felectron-websocket-stream","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fserapath%2Felectron-websocket-stream","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserapath%2Felectron-websocket-stream/lists"}