AGServer
Inherits from
Properties
exchange | A client which lets you interact with pub/sub channels and make RPC requests from the server side. (See API section on the AGExchange object for details). |
clients | An object which holds all fully connected clients on the current server (only those which have completed the handshake). Keys are socket IDs and the values are AGServerSocket instances. |
clientsCount | The number of clients currently connected to this server. |
pendingClients |
An object which holds all pending clients on the current server.
Keys are socket IDs and the values are AGServerSocket instances.
Pending clients are those whose socket.state is 'connecting'; this means
all sockets which are in the middle of the handshake phase.
Once a socket completes its handshake, it will be removed from the pendingClients object
and it will be added to the clients object.
|
pendingClientsCount | The number of pending clients currently connected to this server. |
sourcePort | The public port on which clients connect to SocketCluster (e.g. 80 for HTTP). |
httpServer | An instance of the Node.js HTTP server to which this SC server is attached. |
secure | Whether or not this server uses SSL |
host | The host name for this server |
ackTimeout |
The timeout in milliseconds in which clients have to receive a response to events
which require acknowledgement. For example, when you call
socket.invoke(...) or socket.invokePublish(...) on the client.
|
Events
'error' | This gets triggered when fatal error occurs on this server. The object produced by the listener will have an error property. |
'warning' | A warning is a non-fatal error which does not require restarting the server. AGServerSocket errors are emitted as warnings on the AGServer instance. The object produced by the listener will have a warning property. |
'handshake' |
Emitted as soon as a new AGServerSocket object is created on the server - This occurs at
the beginning of the client handshake, before the 'connection' event.
You should not try to send events to the socket while it is in this state. The object produced by the listener will have a socket property.
|
'connectionAbort' |
Emitted whenever a socket becomes disconnected during the handshake phase. The object produced by the listener will have a socket , code and reason property.
|
'connection' |
Emitted whenever a new socket connection is established with the server (and the handshake has completed). The object produced by the listener will have a socket , id , pingTimeout and isAuthenticated property. If an authentication error occured during the socket handshake phase, the event object will also have an authError property. The socket object of type AGServerSocket and can be used to interact with the corresponding client.
|
'disconnection' |
Emitted whenever a connected socket becomes disconnected (after the handshake phase). Note that if the socket connection was not fully established (e.g. during the SocketCluster handshake phase), then the 'connectionAbort' event will be triggered instead. The object produced by the listener will have a socket , code and reason property.
|
'closure' |
Emitted whenever a connected socket becomes disconnected (at any stage of the handshake/connection cycle). Note that this event is a catch-all for both 'disconnection' and 'connectionAbort' events. The object produced by the listener will have a socket , code and reason property.
|
'subscription' |
Emitted whenever a socket connection which is attached to the server becomes subscribed to a channel. The object produced by the listener will have a socket (AGServerSocket ), a channel and channelOptions property.
|
'unsubscription' |
Emitted whenever a socket connection which is attached to the server becomes unsubscribed from a channel. The object produced by the listener will have a socket and channel property.
|
'authentication' |
Emitted whenever a socket connection which is attached to the server becomes authenticated. The object produced by the listener will have a socket and authToken property.
|
'deauthentication' |
Emitted whenever a socket connection which is attached to the server becomes deauthenticated. The object produced by the listener will have a socket and oldAuthToken property.
|
'authenticationStateChange' |
Triggers whenever the authState of a socket which is attached to the server changes (e.g. transitions between authenticated and unauthenticated states). The object produced by the listener will have a socket , oldAuthState and newAuthState property.
|
'badSocketAuthToken' |
Emitted when a client which is attached to the server tries to authenticate itself with an invalid (or expired) token.
The object produced by the listener will have a socket , authError and signedAuthToken property.
|
'ready' | Emitted when the server is ready to accept connections. |
Methods
constructor(options) |
Create the server instance (with config options).
Note that you can also create the server using the
|
close() |
Close the server and terminate all sockets. |
setMiddleware(type, middlewareFn) |
Lets you set a middleware functions which can be used to manage client access control
to various features of the AGServer. This is useful for monitoring
real-time data flows and also to block access to restricted channels
and resources. Note that only actions from clients pass through middleware.
Server side calls on the AGExchange client such as agServer.exchange.transmitPublish(...) or agServer.exchange.invokePublish(...) do not. See Middleware and authorization guide for a list of all available middleware types and actions.
|
removeMiddleware(type, middlewareFn) |
Lets you remove a middleware function which was previously added using the |
listener(eventName) |
This method returns an event listener stream for the specified See basic usage guide for examples of how to consume listener streams. For more advanced usage, see StreamDemux (this is the library which SocketCluster uses to implement listener streams). |
closeListener(eventName) |
This method will signal to all consuming for-await-of loops (for the eventName listener) to break after they have finished iterating over their current backlogs of events.
|
closeAllListeners() |
This method will signal to all consuming for-await-of loops for all listeners to break after they have finished consuming their respective backlogs of events.
|
killListener(eventName) |
This method will signal to all consuming for-await-of loops for the eventName listener to break immediately and will reset the backpressure for that listener to 0.
|
killAllListeners() |
This method will signal to all consuming for-await-of loops for all listeners to break immediately and will reset the aggregate backpressure for all listeners to 0.
|
getAllListenersBackpressure() | Returns the aggregate backpressure for the server's event listener streams. |
getListenerBackpressure(eventName) |
Get the aggregate backpressure for the eventName listener stream on the server instance. The aggregate backpressure represents the highest backpressure of all consumers for that listener.
|
Stream management methods
These methods should only be used for advanced use cases when you need more control over stream management; for example, when you want to break
out of a specific consumer loop without affecting any other consumer.
These methods can also be useful to check that consumers are being cleaned up properly and to selectively kill specific consumers which are causing backpressure to build up.
For most use cases, you should try to stick to the methods in the table above as it will lead to cleaner logic.
getListenerConsumerStatsList(eventName) |
Get the list of consumers which are consuming data from the specified event listener. This method returns a list of objects which have an id and backpressure property.
|
getAllListenersConsumerStatsList() |
Get the list of all consumers which are consuming data from any listener on the server. This method returns a list of objects which have an id and backpressure property.
|
killListenerConsumer(consumerId) |
This will cause the target listener consumer's for-await-of loop to break immediately.
|