@foxglove/electron-socket

2.1.1 • Public • Published

@foxglove/electron-socket

Networking sockets for Electron apps

npm version

Introduction

Raw sockets are not supported in browser contexts, even in Electron apps. To overcome this limitation, this package uses RPC between the Electron renderer context (referred to in this package as the "renderer" and in Electron documentation as "main world") and the preloader (referred to in Electron documentation as "isolated world" when running with contextIsolation: true) to expose TCP/UDP sockets and server classes in the renderer context. The API somewhat resembles net.Socket/dgram.Socket and net.Server from node.js, with Promise-based methods since these classes are built on asynchronous RPC.

Usage

// preload.ts ////////////////////////////////////////////////////////////////
import { PreloaderSockets } from "@foxglove/electron-socket/preloader";

PreloaderSockets.Create();
// renderer.ts ///////////////////////////////////////////////////////////////
import { Sockets } from "@foxglove/electron-socket/renderer";

async function main() {
  const net = await Sockets.Create();

  const server = await net.createServer();
  server.on("connection", (client) => {
    client.write(new Uint8Array([42]));
  });
  server.listen(9000);

  const socket = await net.createSocket();
  socket.on("data", (data: Uint8Array) => console.log(`Server sent ${data}`));
  socket.connect({ port: 9000, host: "localhost" });
}

main();

License

@foxglove/electron-socket is licensed under the MIT License.

Development

Testing

A small example Electron app is provided in the example directory, which can be used for manual testing. Run it using yarn example.

Releasing

  1. Run yarn version --[major|minor|patch] to bump version
  2. Run git push && git push --tags to push new tag
  3. GitHub Actions will take care of the rest

Stay in touch

Join our Slack channel to ask questions, share feedback, and stay up to date on what our team is working on.

Readme

Keywords

none

Package Sidebar

Install

npm i @foxglove/electron-socket

Weekly Downloads

562

Version

2.1.1

License

MIT

Unpacked Size

220 kB

Total Files

131

Last publish

Collaborators

  • foxglovebot
  • amacneil