How do you send data back in the same stream? Like for a request response pattern.
This is what I have come up with. Is there a better way to do this?
Here I need to keep track of the requests I have sent and to whom and then get a proper response back.
let handler = ({connection, stream}) =>{
await pipe(stream, async (src) => {
for await (const msg of src) {
let data = JSON.parse(msg);
if(data.type == "REQUEST"){
let res = json.stringify(process_func(data))
pipe(res, stream)
}
if(data.type == "RESPONSE" && checkIfIHaveRequestedThisData){
//output data
}
}
});
}
node1.handle('/rpcservice',handler)
node2.handle('/rpcservice',handler)
let { stream } = await node1.dialProtocol(node2, '/rpcservice')
let reqObj = { type : "REQUEST", body : "Foo" }
pipe(JSON.stringify(reqObj), stream)