Server Implmentation
We’ll start with JavaScript. Follow along in the example repo here: https://github.com/backstopmedia/gRPC-book-example/blob/master/examples/nodejs/server.js. We need some generic List
and some Get
functions:
JavaScript
// generic handler for Get* RPCs
const
getHandler
=
(
id
,
recordName
,
dataName
)
=>
{
const
record
=
data
[
dataName
].
find
(
r
=>
r
.
id
===
id
)
if
(
record
)
{
return
{[
recordName
]
:
record
}
}
else
{
return
Promise
.
reject
(
new
NotFoundError
(
`That
${
recordName
}
was not found.`
))
}
}
// generic handler for most List* RPCs
const
listHandler
=
(
recordName
)
=>
({[
recordName
]
:
data
[
recordName
]})
Now, we’ll setup a demo event-emitter for fake Starship
events:
const
starshipTracker
=
emitonoff
()
// fake ship events are happening!
setInterval
(()
=>
{
const
action
=
StarshipActions
[
(
StarshipActions
.
length
*
Math
.
random
())
|
0
]
const
starship
=
data
.
starships
[
(
data
.
starships
.
length
*
Math
.
random
())
|
0
]
starshipTracker...