GithubHelp home page GithubHelp logo

uhst / uhst-client-js Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 2.0 976 KB

User Hosted Secure Transmission JavaScript client library

License: MIT License

HTML 3.23% JavaScript 0.98% TypeScript 95.79%
p2p hacktoberfest uhst uhst-client peers

uhst-client-js's Introduction

uhst-client

JavaScript client library for the User Hosted Secure Transmission framework.

Build Status NPM Version Gitter

Documentation

Visit our website for more complete documentation: https://docs.uhst.io.

Support and discussions

Join us on Gitter or StackOverflow .

Installation

The UHST client library is available on NPM:

$ npm install uhst --save

UMD bundle is also available from the releases tab and can be included with your resources.

Finally, you may skip installing UHST altogether and just load the minified UMD bundle from a CDN:

 <script crossorigin src="https://unpkg.com/uhst/uhst.min.js"></script>

Usage

JavsScript

Assuming you have loaded the library, first create a new instance:

var test = new uhst.UHST();

Refer to the documentation to learn about the options you can pass (including your own relay server URL, WebRTC configuration, etc.) .

Host

Host in UHST is a peer which every other peer connects to. This concept is similar to listen-server in multiplayer games. The simplest way to create a new host is:

var host = test.host();
host.on("ready", () => {
    alert("Host %s ready", host.hostId);
});
host.on("connection", function connection(uhstSocket) {
    uhstSocket.on("message", function incoming(message) {
        console.log("Host received: %s", message);
    });
    uhstSocket.on("open", function ready() {
        // note the socket is ready for sending only
        // after the "open" event fires!
        uhstSocket.send("something");
    });
});

Note that you can optionally specify a requested hostId when calling host() but it may not be accepted by the relay server. You should always use the hostId you get after receiving a ready event when joining the host.
By default the generated host ids will follow this pattern: <4-digit-relay-prefix>-<4-digit-host-id>.
To pass the hostId to a client browser you can embed it in a URL parameter or a QR code.

Client

To connect to a host from another browser use the same hostId you received after ready event:

var client = test.join(<host-id-from-host-ready-above>);
client.on("open", function open() {
    client.send("hello");
});
client.on("message", function incoming(message) {
    console.log("Client received: %s", message);
});

The UHST client interface is similar to the HTML5 WebSocket interface but instead of a dedicated server, one peer acts as a host for other peers to join.

Once a client and a host have connected they can exchange messages asynchronously. Arbitrary number of clients can connect to the same host but clients cannot send messages to each other, they can only communicate with the host.

Contributing

This project is maintained by a community of developers. Contributions are welcome and appreciated. You can find UHST on GitHub; feel free to start an issue or create a pull requests:
https://github.com/uhst/uhst-client-js.

License

Copyright (c) 2021 Stefan Dimitrov
Licensed under MIT License.

uhst-client-js's People

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

thenoim dimitrovs

uhst-client-js's Issues

Implement broadcast

Uhst host should be able to broadcast a message to all connected clients for which the UhstSocket is in ready state.

Enable string compression

Add (optional) compress:true parameter to the UHST constructor options. If enabled, use lz-string to deflate/inflate messages using Uint8Array.

Fix this binding

Currently we are using arrow functions to avoid having to bind this context in every function as they may be used as handlers. Instead, we should add:

this.send = this.send.bind(this);

To the constructor.

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.