@@ -26,54 +26,73 @@ const dataStream = [
2626 * Implements the SayHello RPC method.
2727 */
2828
29-
29+ // Unary stream
30+ // ctx = watch execution context
3031function sayHello ( ctx ) {
32+ // create new metadata
3133 let metadata = new grpc . Metadata ( ) ;
3234 metadata . set ( 'it' , 'works?' )
3335 metadata . set ( 'indeed' , 'it do' )
34- console . dir ( ctx . metadata , { depth : 3 , colors : true } ) ;
36+ // Watcher creates a watch execution context for the watch
37+ // The execution context provides scripts and templates with access to the watch metadata
38+ console . log ( "received metadata from client request" , ctx . metadata )
39+ // console.dir(ctx.metadata, { depth: 3, colors: true });
3540 console . log ( `got sayHello request name: ${ ctx . req . name } ` ) ;
41+
42+ // an alias to ctx.response.res
43+ // This is set only in case of DUPLEX calls, to the the gRPC call reference itself
3644 ctx . res = { message : "Hello " + ctx . req . name } ;
37- // let metadata = new grpc.Metadata();
38- // metadata.set('it', 'does?')
39- metadata . set ( 'UNARY' , 'yes' )
4045
46+ // send response header metadata object directly as an argument and that is set and sent
47+ metadata . set ( 'UNARY' , 'yes' )
4148 ctx . sendMetadata ( metadata )
49+
4250 console . log ( `set sayHello response: ${ ctx . res . message } ` ) ;
4351}
4452
53+ // Server-Side Stream
54+ // used highland library to manage asynchronous data
4555async function sayHellos ( ctx ) {
56+ // create new metadata
4657 let metadata = new grpc . Metadata ( ) ;
4758 metadata . set ( 'it' , 'works?' )
4859 metadata . set ( 'indeed' , 'it do' )
60+ // The execution context provides scripts and templates with access to the watch metadata
4961 console . dir ( ctx . metadata , { depth : 3 , colors : true } ) ;
62+ // converts a request into strings
5063 console . log ( `got sayHellos request name:` , JSON . stringify ( ctx . req , null , 4 ) ) ;
64+
65+ // alias for ctx.request.req
66+ // In case of UNARY and RESPONSE_STREAM calls it is simply the gRPC call's request
5167 let reqMessages = { "message" : 'hello!!! ' + ctx . req . name }
5268 dataStream . push ( reqMessages )
5369 reqMessages = dataStream
54- console . log ( 'what is this?????' , reqMessages )
5570 let streamData = await hl ( reqMessages )
5671 ctx . res = streamData ;
5772 metadata . set ( 'serverStream' , 'indeed' )
5873 dataStream . pop ( )
5974
75+ // send response header metadata object directly as an argument and that is set and sent
6076 ctx . sendMetadata ( metadata )
6177
6278 console . log ( `done sayHellos` ) ;
79+ // ends server stream
6380 ctx . res . end ( )
64- // dataStream = [];
6581}
6682
83+ // Client-Side stream
6784function sayHelloCs ( ctx ) {
85+ // create new metadata
6886 let metadata = new grpc . Metadata ( ) ;
6987 metadata . set ( 'it' , 'works?' )
7088 metadata . set ( 'indeed' , 'it do' )
7189 metadata . set ( 'clientStream' , 'indubitably' )
90+ // The execution context provides scripts and templates with access to the watch metadata
7291 console . dir ( ctx . metadata , { depth : 3 , colors : true } )
7392 console . log ( 'got sayHelloClients' )
74- let counter = 0
75- // console.log("ctx content:",ctx.req)
93+ let counter = 0 ;
7694 let messages = [ ] ;
95+ // client streaming calls to write messages and end writing before you can get the response
7796 return new Promise ( ( resolve , reject ) => {
7897 hl ( ctx . req )
7998 . map ( message => {
@@ -82,10 +101,8 @@ function sayHelloCs (ctx) {
82101 ctx . response . res = { message : 'Client stream: ' + message . name }
83102 messages . push ( message . name )
84103 ctx . sendMetadata ( metadata )
85-
86-
87-
88104 } )
105+ // returns all the elements as an array
89106 . collect ( )
90107 . toCallback ( ( err , result ) => {
91108 if ( err ) return reject ( err )
@@ -97,11 +114,14 @@ function sayHelloCs (ctx) {
97114 } )
98115}
99116
117+ // Bi-Di stream
100118function sayHelloBidi ( ctx ) {
119+ // create new metadata
101120 let metadata = new grpc . Metadata ( ) ;
102121 metadata . set ( 'it' , 'works?' )
103122 metadata . set ( 'indeed' , 'it do' )
104123 console . log ( "got sayHelloBidi" ) ;
124+ // The execution context provides scripts and templates with access to the watch metadata
105125 console . dir ( ctx . metadata , { depth : 3 , colors : true } ) ;
106126 let counter = 0 ;
107127 ctx . req . on ( "data" , d => {
@@ -112,9 +132,10 @@ function sayHelloBidi(ctx) {
112132 } ) ;
113133 metadata . set ( 'bidiStream' , 'ohyes' )
114134 ctx . sendMetadata ( metadata ) ;
135+ // calls end to client before closing server
115136 ctx . req . on ( "end" , ( ) => {
116137 console . log ( `done sayHelloBidi counter ${ counter } ` ) ;
117-
138+ // ends server stream
118139 ctx . res . end ( ) ;
119140 } ) ;
120141}
0 commit comments