anysocket
v0.4.29
An abstract networking layer over multiple transports, client/server agnostic with support for E2EE
This is a work in progress and API is subject to change.
AnySocket.Packer
)/dist/anysocket.browser.js
)** http transport is experimental
Info: Binary RPC arguments and responses are auto packed/unpacked (AnySocket.Packer.pack
/AnySocket.Packer.unpack
).
See benchmarks
npm install --save anysocket
or
<script src="/dist/anysocket.browser.js"></script>
or (if using AnySocketHTTP)
<script src="@anysocket"></script>
The following example starts a websocket server on port 3000.
const AnySocket = require("anysocket");
const server = new AnySocket();
const PORT = 3000;
server.listen("ws", PORT)
.then(() => {
console.log("Listening on port:", PORT);
})
.catch((err) => {
console.error("Failed to start server:", err);
});
server.on("connected", async (peer) => {
console.log("Connected", peer.id);
console.log(await peer.getSyncedTime()) // { time: 1674671482107, rtt: 2, offset: 0 }
peer.send({
hello: "world"
});
});
server.on("message", (packet) => {
console.log("From:", packet.peer.id, "Message:", packet.msg);
});
server.on("disconnected", (peer, reason) => {
console.log("Disconnected", peer.id, "Reason:", reason);
});
The following example connects to a websocket on port 3000
const AnySocket = require("anysocket");
const client = new AnySocket();
const PORT = 3000;
client.connect("ws", "127.0.0.1", PORT)
.then(() => {
console.log("Connected to server");
})
.catch((err) => {
console.error("Failed to connect to server:", err);
});
// after negotiating the AUTH packet, it will trigger the connect event
client.on("connected", async (peer) => {
console.log("Connected", peer.id);
console.log(await peer.getSyncedTime()) // { time: 1674671482107, rtt: 2, offset: 0 }
peer.send({
hello: "world"
});
});
client.on("message", (packet) => {
console.log("From:", packet.peer.id, "Message:", packet.msg);
});
client.on("disconnected", (peer, reason) => {
console.log("Disconnected", peer.id, "Reason:", reason);
});
More in the examples
folder.
See documentation and examples folder
* this will require a change in the protocol, as the protocol assumes the packets are sent using a reliable, ordered connection
MIT