0.0.5 • Published 2 years ago
@nixjs23n6/grpc-websocket v0.0.5
@nixjs23n6/grpc-websocket
A small wrapper for integrating Axios to Typescript
Quick Setup
Install
Install these dependencies:
yarn add @nixjs23n6/grpc-websocket
Setup & Usage
Normally
import ProtoJSON from "./proto.json";
import ProtoFile from "./proto.proto";
import {
WSClient,
ConstantBackOff,
Utils,
WSEnums,
proto,
} from "@nixjs23n6/grpc-websocket";
const accessToken = "";
const HOST = "localhost:4444";
const baseURL = "ws://" + HOST;
const ws = new WSClient(
"ws://" + HOST,
undefined,
ProtoFile,
ProtoJSON,
"/ws",
new ConstantBackOff(1000),
undefined,
(websocket: WSClient) => {
if (
websocket.protocols &&
Utils.isTokenExpired(String(websocket.protocols))
) {
console.log("Token expired");
}
}
);
ws.connect("/ws", accessToken);
ws.subscribeState((state: WSEnums.States) => {
if (state === WSEnums.States.ON_OPENED) {
console.log("connected to websocket");
console.log("proto.protoRoot");
console.log(proto.protoRoot);
}
})
Builder
import ProtoJSON from "./proto.json";
import {
WSClientBuilder,
ConstantBackOff,
Utils,
WSEnums,
proto,
} from "@nixjs23n6/grpc-websocket";
const accessToken = "";
const HOST = "localhost:4444";
const baseURL = "ws://" + HOST;
const ws = new WSClientBuilder(baseURL, "/ws")
.addProtocols(accessToken)
.addProto(ProtoFile)
.addProtoJSONFallback(ProtoJSON)
.addBackOff(new ConstantBackOff(1000))
.addExecuteAnyFunc((websocket) => {
if (
websocket.protocols &&
Utils.isTokenExpired(String(websocket.protocols))
) {
console.log("Token expired");
}
})
.connect();
ws.subscribeState((state: WSEnums.States) => {
if (state === WSEnums.States.ON_OPENED) {
console.log("connected to websocket");
console.log("proto.protoRoot");
console.log(proto.protoRoot);
}
})