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 servicegrpc~ServiceDefinition The service descriptor
implementationObject.<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 portstring The port that the server should bind on, in the format "address:port"
credsgrpc.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 portstring The port that the server should bind on, in the format "address:port"
credsgrpc.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 namestring The name of the method that the provided function should handle/respond to.
handlergrpc.Server~handleCall Function that takes a stream of request values and returns a stream of response values
serializegrpc~serialize Serialization function for responses
deserializegrpc~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 callbackfunction The shutdown complete callback
Type Definitions
-
bindCallback( [error], port)
-
Called with the result of attempting to bind a port
Parameters:
Name Type Argument Description errorError <optional>
If non-null, indicates that binding the port failed.
portnumber 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 callgrpc~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 callgrpc~ServerReadableStream The call object
callbackgrpc.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 callgrpc~ServerWritableStream The call object
-
handleUnaryCall(call, callback)
-
User-provided method to handle unary requests on a server
Parameters:
Name Type Description callgrpc~ServerUnaryCall The call object
callbackgrpc.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 errorgrpc~ServiceError An error, if the call failed
value* The response value. Must be a valid argument to the
responseSerializemethod of the method that is being handledtrailergrpc.Metadata <optional>
Trailing metadata to send, if applicable
flagsgrpc.writeFlags <optional>
Flags to modify writing the response