BestWS
The best WebSocket library for fast, small send size, binary protocols.
Long gone is Socket.io's slow and stringified packets.
Time to use small and fast binary protocols
Quick server-client example
const bws = require("../src/export.js");
const server = new bws.Server(bws.new.HTTP(3000));
server.on("initialize", (ws, res, init, req) => {
console.log("Initial data received: ", init);
res(["I", { got: "the" }, "message", 1111, [",", "you", { may: "connect" }, true]]);
ws.on("message", (data) => {
console.log("Got data from client: ", data);
ws.send("Hello!");
});
});
server.on("listening", () => {
console.log("Listening.");
const client = new bws.Client("ws://localhost:3000", [1, { a: "Init request data" }, 2]);
client.on("initializing", () => console.log("Client connecting..."));
client.on("open", (init) => {
console.log("Server sent their own initial data: ", init);
client.send("Hi!");
});
client.on("message", (data) => {
console.log("Got data from server: ", data);
});
});
Where this library shines
The variables/class stored in BestWS.buffer
is what makes this library shine.
The encoding and binary functionality is beyond every other library on npm, especially Socket.io.
The socket part is just a wrapper mostly of node ws.
Documentation
All documentation shown below will be only the binary/encoding functionality, for the rest just look at the source or examples.
Encoding to a buffer/binary
const bestEncoder = require("../src/export.js").buffer;
const writer = new bestEncoder.Writer(1000);
const data = {
hello: "byte",
one: 2,
3: "four",
array: [
5,
{
object: true,
},
6,
false,
true,
[7, 8],
],
buffer: new Uint8Array([9, 10, 11, 12]),
float: 13.1,
};
writer.write(data);
const encoded = writer.buffer;
const reader = new bestEncoder.Reader(encoded);
const decoded = reader.read();
decoded === data;
Bi-encoding data
const bestEncoder = require("../src/export.js").buffer;
const writer = new bestEncoder.Writer(1000);
writer.write({
object: true,
array: false,
});
writer.write([false, true]);
const reader = new bestEncoder.Reader(writer.buffer);
reader.read();
reader.read();
Warning
I didn't make the above work with browsers, NodeJS only.
Please!
If you want to, make this protocol or encoder in your own language.
Eventually the horrors and optimized garbage that is socket.io will disappear.