I m trying to send an array data object from the server.js to the client.js but I get undefined.
What is the correct way to send this data types
- string
- number
- Object
- array
I m trying to send a simple array data from the master to the client. I dont want to even imagine to send a more complex data like Object
Can someone show me, a simple working example where I can send from the server.js this data
[
{ id: '1', title: 'Note 1', content: 'Content 1'},
{ id: '2', title: 'Note 2', content: 'Content 2'},
{ id: '3', title: 'Note 3', content: 'Content 3'}
]
and on the client, I want to see this response if I run
node .\client.js
[
{ id: '1', title: 'Note 1', content: 'Content 1'},
{ id: '2', title: 'Note 2', content: 'Content 2'},
{ id: '3', title: 'Note 3', content: 'Content 3'}
]
notes.proto
syntax = "proto3";
package notes;
service NoteService {
rpc GetNoteList (Empty) returns (NoteList) {} <--- this not workig
rpc GetNoteItem (Empty) returns (Note) {} <--- this works
}
message Empty {}
message Note {
string id = 1;
string title = 2;
string content = 3;
}
message NoteList {
repeated Note notes = 1;
}
server.js
const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');
const PROTO_PATH = __dirname + '/../../protos/notes.proto';
// const notesProto = grpc.load('notes.proto')
const packageDefinition = protoLoader.loadSync(
PROTO_PATH,
{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
}
);
const notesProto = grpc.loadPackageDefinition(packageDefinition).notes;
// returns a list of notes.
const getNoteList = (call, callback) => {
// mock data
const notes = [
{ id: '1', title: 'Note 1', content: 'Content 1'},
{ id: '2', title: 'Note 2', content: 'Content 2'},
{ id: '3', title: 'Note 3', content: 'Content 3'},
];
callback(null, { message: notes });
}
function getNoteItem(call, callback) {
const data = { id: '1', title: 'Note 1', content: 'Content 1'};
return callback(null, data)
}
/**
* Starts an RPC server that receives requests for the Greeter service at the
* sample server port
*/
function main() {
var server = new grpc.Server();
server.addService(notesProto.NoteService.service, {
GetNoteList: getNoteList,
GetNoteItem: getNoteItem
});
server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());
console.log('Server running at http://127.0.0.1:50051')
server.start();
}
main();
client.js
// var PROTO_PATH = __dirname + '/../../protos/model.proto';
var PROTO_PATH = __dirname + '/../../protos/notes.proto';
var grpc = require('grpc');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
PROTO_PATH,
{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
}
);
const notesProto = grpc.loadPackageDefinition(packageDefinition).notes;
function main() {
const client = new notesProto.NoteService('localhost:50051', grpc.credentials.createInsecure());
var user;
if (process.argv.length >= 3) {
user = process.argv[2];
} else {
user = 'world';
}
// console.log({user : user});
// expected to return array of objects
client.getNoteList('test', (err, res) => {
if (!err) {
console.log('list1: ', res);
console.log('list2: ', res.message);
} else {
console.error(err);
}
});
// get a single item
client.getNoteItem('test', (err, res) => {
if (!err) {
console.log('getNoteItem res: ', res);
} else {
console.error(err);
}
});
}
main();
output
PS C:\dev\george\tests\GRPC\grpc-test\server\node> node .\client.js
getNoteItem res: { id: '1', title: 'Note 1', content: 'Content 1' }
list1: { notes: [] }
list2: undefined