new Server( [options])
Constructs a server object that stores request handlers and delegates incoming requests to those handlers
Parameters:
Name | Type | Argument | Description |
---|---|---|---|
options |
Object |
<optional> |
Options that should be passed to the internal server implementation. The available options are listed in this document. |
Example
var server = new grpc.Server(); server.addProtoService(protobuf_service_descriptor, service_implementation); server.bind('address:port', server_credential); server.start();
Members
-
addProtoService
-
Add a proto service to the server, with a corresponding implementation
- Deprecated:
-
- Use grpc.Server#addService instead
Methods
-
addService(service, implementation)
-
Add a service to the server, with a corresponding implementation.
Parameters:
Name Type Description service
grpc~ServiceDefinition The service descriptor
implementation
Object.<String, grpc.Server~handleCall> Map of method names to method implementation for the provided service.
-
bind(port, creds)
-
Binds the server to the given port, with SSL disabled if creds is an insecure credentials object
Parameters:
Name Type Description port
string The port that the server should bind on, in the format "address:port"
creds
grpc.ServerCredentials Server credential object to be used for SSL. Pass an insecure credentials object for an insecure port.
Returns:
The bound port number. Negative if binding the port failed.
- Type
- number
-
bindAsync(port, creds)
-
Binds the server to the given port, with SSL disabled if creds is an insecure credentials object. Provides the result asynchronously.
Parameters:
Name Type Description port
string The port that the server should bind on, in the format "address:port"
creds
grpc.ServerCredentials Server credential object to be used for SSL. Pass an insecure credentials object for an insecure port.
-
forceShutdown()
-
Forcibly shuts down the server. The server will stop receiving new calls and cancel all pending calls. When it returns, the server has shut down. This method is idempotent with itself and tryShutdown, and it will trigger any outstanding tryShutdown callbacks.
-
register(name, handler, serialize, deserialize, type)
-
Registers a handler to handle the named method. Fails if there already is a handler for the given method. Returns true on success
Parameters:
Name Type Description name
string The name of the method that the provided function should handle/respond to.
handler
grpc.Server~handleCall Function that takes a stream of request values and returns a stream of response values
serialize
grpc~serialize Serialization function for responses
deserialize
grpc~deserialize Deserialization function for requests
type
'unary' | 'client_stream' | 'server_stream' | 'bidi' The streaming type of method that this handles
Returns:
True if the handler was set. False if a handler was already set for that name.
- Type
- boolean
-
start()
-
Start the server and begin handling requests
-
tryShutdown(callback)
-
Gracefully shuts down the server. The server will stop receiving new calls, and any pending calls will complete. The callback will be called when all pending calls have completed and the server is fully shut down. This method is idempotent with itself and forceShutdown.
Parameters:
Name Type Description callback
function The shutdown complete callback
Type Definitions
-
bindCallback( [error], port)
-
Called with the result of attempting to bind a port
Parameters:
Name Type Argument Description error
Error <optional>
If non-null, indicates that binding the port failed.
port
number The bound port number. If binding the port fails, this will be negative to match the output of bind.
-
handleBidiStreamingCall(call)
-
User provided method to handle bidirectional streaming calls on the server.
Parameters:
Name Type Description call
grpc~ServerDuplexStream The call object
-
handleCall
-
Unified type for application handlers for all types of calls
Type:
- grpc.Server~handleUnaryCall | grpc.Server~handleClientStreamingCall | grpc.Server~handleServerStreamingCall | grpc.Server~handleBidiStreamingCall
-
handleClientStreamingCall(call, callback)
-
User provided method to handle client streaming methods on the server.
Parameters:
Name Type Description call
grpc~ServerReadableStream The call object
callback
grpc.Server~sendUnaryData The callback to call to respond to the request
-
handleServerStreamingCall(call)
-
User provided method to handle server streaming methods on the server.
Parameters:
Name Type Description call
grpc~ServerWritableStream The call object
-
handleUnaryCall(call, callback)
-
User-provided method to handle unary requests on a server
Parameters:
Name Type Description call
grpc~ServerUnaryCall The call object
callback
grpc.Server~sendUnaryData The callback to call to respond to the request
-
sendUnaryData(error, value [, trailer] [, flags])
-
Callback function passed to server handlers that handle methods with unary responses.
Parameters:
Name Type Argument Description error
grpc~ServiceError An error, if the call failed
value
* The response value. Must be a valid argument to the
responseSerialize
method of the method that is being handledtrailer
grpc.Metadata <optional>
Trailing metadata to send, if applicable
flags
grpc.writeFlags <optional>
Flags to modify writing the response