Inherits from
Properties
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.
socket.invoke(...) or socket.invokePublish(...) on the client.
Events
error property.
AGServerSocket errors are emitted as warnings on the AGServer instance. The object produced by the listener will have a warning property.
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.
socket, code and reason property.
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.
'connectionAbort' event will be triggered instead. The object produced by the listener will have a socket, code and reason property.
'disconnection' and 'connectionAbort' events. The object produced by the listener will have a socket, code and reason property.
socket (AGServerSocket), a channel and channelOptions property.
socket and channel property.
socket and authToken property.
socket and oldAuthToken property.
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.
socket, authError and signedAuthToken property.
Methods
Create the server instance (with config options).
Note that you can also create the server using the attach method of the top-level socketClusterServer instance.
The options object can have any of the following properties (sample showing defaults):
{
// An instance of a Node.js HTTP server.
// https://nodejs.org/api/http.html#http_class_http_server
// This option should not be set if the server is created
// with socketClusterServer.attach(...).
httpServer: null,
// This can be the name of an npm module or a path to a
// Node.js module to use as the WebSocket server engine.
wsEngine: 'ws',
// Custom options to pass to the wsEngine when it is being
// instantiated.
wsEngineServerOptions: {},
// The key which SC will use to encrypt/decrypt authTokens,
// defaults to a 256 bits cryptographically random hex
// string. The default JWT algorithm used is 'HS256'.
// If you want to use RSA or ECDSA, you should provide an
// authPrivateKey and authPublicKey instead of authKey.
authKey: null,
// perMessageDeflate compression. Note that this option is
// passed directly to the wsEngine's Server object.
// So if you're using 'ws' as the engine, you can pass an
// object instead of a boolean.
// Note that by default, per-message deflate only kicks in
// for messages > 1024 bytes.
perMessageDeflate: false,
// If using an RSA or ECDSA algorithm to sign the
// authToken, you will need to provide an authPrivateKey
// and authPublicKey in PEM format (string or Buffer).
authPrivateKey: null,
authPublicKey: null,
// The default expiry for auth tokens in seconds
authDefaultExpiry: 86400,
// The algorithm to use to sign and verify JWT tokens.
authAlgorithm: 'HS256',
// Can be 1 or 2. Version 1 is for maximum backwards
// compatibility with SocketCluster clients.
protocolVersion: 2,
// In milliseconds - If the socket handshake hasn't been
// completed before this timeout is reached, the new
// connection attempt will be terminated.
handshakeTimeout: 10000,
// In milliseconds, the timeout for receiving a response
// when using invoke() or invokePublish().
ackTimeout: 10000,
// Origins which are allowed to connect to the server.
origins: '*:*',
// The maximum number of unique channels which a single
// socket can subscribe to.
socketChannelLimit: 1000,
// The interval in milliseconds on which to
// send a ping to the client to check that
// it is still alive.
pingInterval: 8000,
// How many milliseconds to wait without receiving a ping
// before closing the socket.
pingTimeout: 20000,
// Whether or not an error should be emitted on
// the socket whenever an action is blocked by a
// middleware function
middlewareEmitFailures: true,
// The URL path reserved by SocketCluster clients to
// interact with the server.
path: '/socketcluster/',
// Whether or not clients are allowed to publish messages
// to channels.
allowClientPublish: true,
// Whether or not to batch all socket messages
// for some time immediately after completing
// a handshake. This can be useful in failure-recovery
// scenarios (e.g. batch resubscribe).
batchOnHandshake: false,
// If batchOnHandshake is true, this lets you specify
// How long to enable batching (in milliseconds) following
// a successful socket handshake.
batchOnHandshakeDuration: 400,
// If batchOnHandshake is true, this lets you specify
// the size of each batch in milliseconds.
batchInterval: 50,
// Lets you specify the default cleanup behaviour for
// when a socket becomes disconnected.
// Can be either 'kill' or 'close'. Kill mode means
// that all of the socket's streams will be killed and
// so consumption will stop immediately.
// Close mode means that consumers on the socket will
// be able to finish processing their stream backlogs
// bebfore they are ended.
socketStreamCleanupMode: 'kill'
}
Close the server and terminate all sockets.
agServer.exchange.transmitPublish(...) or agServer.exchange.invokePublish(...) do not. See Middleware and authorization guide for a list of all available middleware types and actions.
Lets you remove a middleware function which was previously added using the setMiddleware(...) method.
This method returns an event listener stream for the specified eventName. This object is an asyncIterable which can be consumed with a for-await-of loop.
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).
for-await-of loops (for the eventName listener) to break after they have finished iterating over their current backlogs of events.
for-await-of loops for all listeners to break after they have finished consuming their respective backlogs of events.
for-await-of loops for the eventName listener to break immediately and will reset the backpressure for that listener to 0.
for-await-of loops for all listeners to break immediately and will reset the aggregate backpressure for all listeners to 0.
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.
id and backpressure property.
id and backpressure property.
for-await-of loop to break immediately.