Skip to content

Instantly share code, notes, and snippets.

@devarajchidambaram
Last active March 12, 2019 04:57
Show Gist options
  • Save devarajchidambaram/8221c5628082fdc95bffa1f9a8dfe316 to your computer and use it in GitHub Desktop.
Save devarajchidambaram/8221c5628082fdc95bffa1f9a8dfe316 to your computer and use it in GitHub Desktop.
grpc in nodejs
const grpc = require('grpc')
const PROTO_PATH = './notes.proto'
const NoteService = grpc.load(PROTO_PATH).NoteService
const client = new NoteService('localhost:50051',
grpc.credentials.createInsecure())
console.log('client', client)
module.exports = client
client.list({}, (error, notes) => {
if (!error) {
console.log('successfully fetch List notes')
console.log(notes)
} else {
console.error(error)
}
})
const grpc = require('grpc')
const notesProto = grpc.load('notes.proto')
const uuidv1 = require('uuid/v1')
const notes = [
{ id: '1', title: 'Note 1', content: 'Content 1'},
{ id: '2', title: 'Note 2', content: 'Content 2'}
]
const server = new grpc.Server()
server.addService(notesProto.NoteService.service, {
list: (_, callback) => {
callback(null, notes)
},
insert: (call, callback) => {
let note = call.request
note.id = uuidv1()
notes.push(note)
callback(null, note)
}
})
server.bind('127.0.0.1:50051', grpc.ServerCredentials.createInsecure())
console.log('Server running at http://127.0.0.1:50051')
server.start()
syntax = "proto3";
service NoteService {
rpc List (Empty) returns (NoteList) {}
rpc Insert (Note) returns (Note) {}
}
message Empty {}
message Note {
string id = 1;
string title = 2;
string content = 3;
}
message NoteList {
repeated Note notes = 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment