Giter Club home page Giter Club logo

electron-socket's Introduction

@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.

electron-socket's People

Contributors

amacneil avatar defunctzombie avatar esthersweon avatar foxymiles avatar jhurliman avatar jtbandes avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

emmkimme

electron-socket's Issues

Change direction of socket registration.

The current socket registration flow has preloader sending setup to renderer. This requires a user to call socket creation early and at the right time in renderer. This is fragile since preloader always starts and loads before renderer. Any hiccup with renderer startup could result in socket setup in renderer hanging.

Instead we should have renderer talk to preloader. Since preloader is always run before renderer it is more likely and easier to ensure the preloader message receivers are setup before any renderer code is run.

Create unit and integration tests

This library has no test coverage right now and is susceptible to regressions that only show up when you try to use the ROS 1 Native connector in Foxglove Studio.

Since this library is intrinsically tied to Electron implementation details, for full coverage we need an integration test that compiles a small electron app, runs it, and confirms socket and server creation and communication works as expected.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.