Last active
March 12, 2019 04:57
-
-
Save devarajchidambaram/8221c5628082fdc95bffa1f9a8dfe316 to your computer and use it in GitHub Desktop.
grpc in nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
}) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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