Giter Club home page Giter Club logo

pojntfx / unisockets Goto Github PK

View Code? Open in Web Editor NEW
34.0 5.0 6.0 3.1 MB

A universal Berkeley sockets implementation for both WebAssembly (based on WebRTC) and native platforms with bindings for C, Go and TinyGo.

Home Page: https://alphahorizonio.github.io/unisockets/

License: GNU Affero General Public License v3.0

Makefile 6.70% TypeScript 59.02% C 11.31% Go 14.17% JavaScript 8.51% HTML 0.27% Procfile 0.01%
berkeley-sockets bsd-sockets posix-sockets unisockets tinynet c tinygo go webassembly wasm

unisockets's Introduction

unisockets

A universal Berkeley sockets implementation for both WebAssembly (based on WebRTC) and native platforms with bindings for C, Go and TinyGo.

Yarn CI make CI Mirror TypeDoc PkgGoDev npm Minimal Demo Lite (webnetes) Demo Full (webnetes) Demo Part of webnetes

Overview

unisockets implements the Berkeley sockets API. On a native environment like Linux, it falls back to the native Berkeley sockets API; on WASM it uses WebRTC for fast peer-to-peer communication instead of the (non-available) native API. This allows you to "just recompile" an existing socket server/client (such as a web server etc.) and run it natively, in a WebAssembly runtime or in the browser, without the need for a WebSocket proxy like in emscripten or some other proxy mechanism. You've heard that right, this library allows you to bind in the browser!

Components

UML Diagram

The system is made up of the following components:

  • Signaling: A WebRTC signaling server (with two implementations), client and protocol has been implemented to allow for nodes to discover each other and exchange candidates, but is not involved in any actual connections. When compiling natively, it is not required.
  • Transport: A minimal wrapper around the WebRTC API. When compiling to WASM, this component manages all actual data transfers and handles incoming/outgoing peer to peer connections. When compiling natively, it is not required.
  • Sockets: A set of WebAssembly imports that satisfy the basic API of the Berkeley sockets, such as socket, bind, listen, accept, connect, send, recv etc. When compiling natively, it falls back to the native implementation.

These components have no hard dependencies on one another, and can be used independendly.

Additionally, a universal C/C++ header for easy usage and Go/TinyGo bindings (see PkgGoDev) have been created.

Signaling Protocol

The signaling components use the following protocol:

Sequence Diagram

There are two implementations of the signaling server. The TypeScript version is maintained is this repo, the Java version can be found in junisockets.

Public signaling server instances:

Implementation URL
TypeScript wss://signaler.webnetes.dev
Java wss://jsignaler.webnetes.dev

Further Resources

Interested in an implementation of the Go net package based on this package, with TinyGo and WASM support? You might be interested in tinynet!

You want a Kubernetes-style system for WASM, running in the browser and in node? You might be interested in webnetes, which uses unisockets for it's networking layer.

Usage

Check out the universal C/C++ header for the C API docs or PkgGoDev for the Go/TinyGo API. Many examples on how to use it (C, TinyGo & Go clients & servers plus an example WebAssembly runner) can also be found in the cmd folder. Looking for advice on how to build and run natively or using WASM? Check out the Makefile!

License

unisockets (c) 2021 Felicitas Pojtinger and contributors

SPDX-License-Identifier: AGPL-3.0

unisockets's People

Contributors

imyth avatar jakwai01 avatar pojntfx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

unisockets's Issues

not working in the envoy

due to the issue, I managed to build it with tinygo 0.17.0, and it's succeed.

build command:

tinygo build -o build/socket.wasm -target=wasi -wasm-abi=generic -cflags "-DUNISOCKETS_WITH_CUSTOM_ARPA_INET" .

but, it's failed running in Istio with the proxy of envoy:

2021-08-16T02:52:42.942205Z	error	envoy wasm	Failed to load Wasm module due to a missing import: env.unisockets_socket
2021-08-16T02:52:42.942234Z	error	envoy wasm	Failed to load Wasm module due to a missing import: env.unisockets_connect
2021-08-16T02:52:42.942238Z	error	envoy wasm	Failed to load Wasm module due to a missing import: env.unisockets_send
2021-08-16T02:52:42.942240Z	error	envoy wasm	Failed to load Wasm module due to a missing import: env.unisockets_recv
2021-08-16T02:52:42.942247Z	error	envoy wasm	Wasm VM failed Failed to initialize Wasm code
2021-08-16T02:52:42.942772Z	critical	envoy wasm	Plugin configured to fail closed failed to load
2021-08-16T02:52:42.942787Z	critical	envoy wasm	Plugin configured to fail closed failed to load
2021-08-16T02:52:42.942793Z	critical	envoy wasm	Plugin configured to fail closed failed to load
2021-08-16T02:52:42.965088Z	warning	envoy config	gRPC config for type.googleapis.com/envoy.config.listener.v3.Listener rejected: Error adding/updating listener(s) 10.107.169.228_9091: Unable to create Wasm network filter tcp-socket-replace

not working in go ubuntu/amd64

[root@linux164 tinygo]# tinygo build -o build/socket.wasm -target=wasi -wasm-abi=generic .
# github.com/alphahorizonio/unisockets/pkg/unisockets
../../../../usr/local/lib/tinygo/lib/wasi-libc/sysroot/include/__header_sys_socket.h:12:35: unexpected token ILLEGAL, expected end of expression
[root@linux164 tinygo]#  go version
go version go1.16.5 linux/amd64

[root@linux164 tinygo]# tinygo version
tinygo version 0.19.0 linux/amd64 (using go version go1.16.5 and LLVM version 11.0.0)

[root@linux164 tinygo]# cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.5 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.5 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

Doesn't compile for wasm & tinygo

$ tinygo build -wasm-abi=generic -target=wasi -o main.wasm main.go
# github.com/alphahorizonio/unisockets/pkg/unisockets
../../../../../../../usr/local/lib/tinygo/lib/wasi-libc/sysroot/include/__header_sys_socket.h:12:35: unexpected token ILLEGAL, expected end of expression

Using go1.17.7, tinygo 0.22.0

Specifically, trying to compile an application which uses your TinyNet from a project I created, TinyWWW. Works fine when compiling an ELF on Ubuntu 20.04, but Wasm seems to not be happy.

not woring in go darwin/amd64

../../../../golang/gopath/pkg/mod/github.com/alphahorizonio/[email protected]/pkg/unisockets/unisockets_native_posix_go.go:116:16: could not determine kind of name for C.htons

go version: go1.16.4 darwin/amd64

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.