communicate JSON and other information using http2
npm install @trevthedev/http2communicatorJSON and stream messaging over HTTP2.
``shell`
npm install @trevthedev/http2communicator
On the Client
`javascript`
const response = await client.ask({ whatever: 'question or resource request' })
On the Server
`javascript`
server.on('question', async (serverResponse)=>{
serverResponse.reply({ response: 'what ever' })
})
`javascript
import ServerNode from '@trevthedev/http2communicator'
const server = new ServerNode()
server.on('question', async (serverResponse) => {
console.log(LOG STEP 1: ${JSON.stringify(serverResponse.json)})LOG STEP 4: ${JSON.stringify(msg)}
// handles any messages sent by client of type 'message' to this serverResponse
serverResponse.on('message', (msg) => console.log())LOG STEP 5: ${JSON.stringify(msg)}
// sends 'hello' JSON message to client's question
serverResponse.say({ first: 'your', name: 'please', step: 2 }, 'hello')
// asks a question of client's question
const question = serverResponse.ask({
do: 'you', like: 'your', name: ['yes', 'no'], step: 3,
})
// handles any messages sent by client of type 'message' to this question
question.on('message', (msg) => {
console.log()LOG STEP 7: ${JSON.stringify(await question)}
question.say({ and: 'I', say: 'more', step: 6 }, 'more')
})
// waits for client to respond to question
console.log()
// establishes a new stream from client to server (opposite of Push Stream)
// streams can also stream objects - known as Speaker
const incomingStream = await serverResponse.createListener('uploadFile', 'raw')
incomingStream.pipe(process.stdout)
// stream file to client - Push Stream
// streams can also stream objects - known as Speaker
const fileSpeaker = await serverResponse.createSpeaker('downloadFile', 'raw')
fs.createReadStream('package.json').pipe(fileSpeaker)
// after file has been streamed, reply to the original question
fileSpeaker.on('finish', () => serverResponse.reply({ my: 'name', is: 'server' }))
})
server.listen()
`
`javascript
import ClientNode from '@trevthedev/http2communicator/client'
const client = new ClientNode()
// ask for something from the server (question)
const question = client.ask({ what: 'is', your: 'name', step: 1 })
// handles any messages sent by server of type 'hello' to this question
question.on('hello', (msg) => {
console.log(LOG STEP 2: ${JSON.stringify(msg)})
// sends message to server of type 'message' to this question
question.say({ i: 'say', stuff: true, step: 4 })
})
// handles any questions from the server to this question
// Response object is provided
question.on('question', (response) => {
console.log(LOG STEP 3: ${JSON.stringify(response.json)})LOG STEP 6: ${JSON.stringify(msg)}
// handles any messages sent by server of type 'more' to this response
response.on('more', (msg) => {
console.log()
// reply to servers question
response.reply({
answer: 'yes', i: 'like', my: 'name', step: 7,
})
})
// sends message to server of type 'message' to this response
response.say({ i: 'also', say: 'stuff', step: 5 })
})
// stream established by server.createListener
question.on('uploadFile', (stream) => {
fs.createReadStream('package.json').pipe(stream)
})
// stream established by server.createSpeaker
question.on('downloadFile', (stream) => {
stream.pipe(process.stdout)
});
(async () => {
// await response to question
const response = await question
console.log(LOG STEP 1 ANSWER: ${JSON.stringify(response)})`
})()
The host of the http2 server.
Instantiation:
`javascript
import ServerNode from '@TrevTheDev/http2Communicator'
const server = new ServerNode(http2Server, settings)
`
- http2Server \settings
- \
#### serverNode.listen(port, hostname)
starts server listening
- port \hostname
- \
#### serverNode.gracefulShutdown()
- returns \
A client the connects to a ServerNode.
Instantiation:
`javascript
import ClientNode from '@TrevTheDev/http2Communicator/client'
const client = new ClientNode(settings)
`
- settings \
#### clientNode.ask(json)
Asks a new Question of the ServerNode
- json \Question promise that resolves after the response is received
#### clientNode.end()
- returns \
Extends EventEmitter
Instantiation:
`javascript
clientNode.ask(Json)
serverResponse.ask(Json)
`
A Question is a request json Promise that awaits a response json. On await the Question is sent.
- returns \
#### Properties
- id \objectStream
- \json
- \response \speakers
- \Speakersresponse
- \Response - a "sub" question
#### Messages Handled
- reply on receipt this resolves the Question Promise. It will throw if there are any open Speakerscancelled
- on receipt this rejects the Question Promise with cancelled message. It will throw if there are any open Speakersquestion
- emits a question event with a new Response objectlistening
- emits a speakerType event with a Speaker |Stream objectemit
- any messages not of the above type are ed as their message type
Sends a json object of type via the ObjectStream. Say will only work after question has been sent to server via await.
Extends Response
Instantiation:
`javascript
serverNode.on('question', (serverResponse) => {
})
`
#### Properties
- speakers \Speakers|StreamsListeners
- \Speakers|Streams
Creates a new Speaker|Stream that the serverReponse can send either Objects on or anything else.
- speakerName \speakerType
- \Speaker if 'object' or a Stream if 'raw'optional
- \Speaker will emit speakerType event on stream
- returns \
Creates a new listening Speaker|Stream that the serverReponse can receive either Objects on or anything else.
- speakerName \speakerType
- \Speaker if 'object' or a Stream if 'raw'Speaker
- returns \ or a Stream
Extends EventEmitter
Instantiation:
`javascript
question.on('question', (response) => {
})
`
#### Properties
- id \objectStream
- \json
- \Questionquestions
- \Questions asked by this Response awaiting answers
#### Messages Handling
- any messages received are emited as their message type
Sends json response to this Question and resolves promise. Type can also be cancelled to reject the Question. Throws if any Questions, Speakers or Listeners remain connected.
- json \type \
Only one reply should be sent.
Asks a new Question related to this Response
- json \question to askQuestion
- returns \ promise that resolves after the response is received
Sends a json object of type via the ObjectStream
An object stream converts a stream of bytes into a stream of JSON objects. It emits a new event object for each object received
- stream \eventTarget
- \object, end, finish, closed events
Instantiation via serverResponse.createSpeaker and serverResponse.createListener
Sends JSON objects in one direction using an ObjectStream
sends json over ObjectStream
sends json over ObjectStream and ends ObjectStream`