Inherits from
Properties
null initially and will change each time a new underlying connection is made.
socket.CONNECTING, socket.OPEN or socket.CLOSED.
socket.AUTHENTICATED) or 'unauthenticated' (socket.UNAUTHENTICATED).
null if no token is associated with this socket.
null if no token is associated with this socket.
Events
error property which contains an error object.
Emitted whenever the socket connects to the server (includes reconnections).
The object produced by the listener will have an id, isAuthenticated, authToken, pingTimeout and processPendingSubscriptions property. If an issue occurred as part of the handshake, there will also be an authError property which will hold an error object.
The processPendingSubscriptions property represents a callback function which, when called, will send all pending channel subscriptions to the server (to activate pending channels).
Note that this processPendingSubscriptions callback will only work if the client socket.options.autoSubscribeOnConnect option is set to false. See SocketCluster client API.
code and reason property.
socket.disconnect() while the socket is connecting. The object produced by the listener will have a code and reason property.
'disconnect' and 'connectAbort'. The object produced by the listener will have a code and reason property.
autoConnect option
to false when you create the socket with socketClusterClient.create(...) - You will need to register the handler
before you call socket.connect().
socket.send(...). The object produced by the listener will have a message property.
channel and message property.
channel and subscriptionOptions property.
error, channel and subscriptionOptions property.
channel property.
socket.AUTHENTICATED and socket.UNAUTHENTICATED states.
The object produced by the listener will have at least two properties: oldAuthState and newAuthState.
If newAuthState is 'authenticated', the object will also have an additional signedAuthToken property which
will be the base64 signed JWT auth token as a string and an authToken property which will represent the token as a plain Object.
channel, oldChannelState, newChannelState and subscriptionOptions property.
channel and subscriptionOptions property.
signedAuthToken and authToken property.
oldSignedAuthToken and oldAuthToken property.
message property.
Errors
For the list of all SocketCluster errors (and their properties) see sc-errors.
To check the type of an error in SocketCluster, you should use the name property of the error (do not use the instanceof statement).
Errors which are sent to the client from the server will be dehydrated on the server and rehydrated on the client - As a result, they will be cast
to plain Error objects.
Methods
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.
receiverName. This object is an asyncIterable which can be consumed with a for-await-of loop.
for-await-of loops (for the receiverName receiver) to break after they have finished iterating over their current backlogs of data.
for-await-of loops for all receivers to break after they have finished consuming their respective backlogs of data.
for-await-of loops for the receiverName receiver to break immediately and will reset the backpressure for that receiver to 0.
for-await-of loops for all receivers to break immediately and will reset the aggregate backpressure for all receivers to 0.
procedureName. This object is an asyncIterable which can be consumed with a for-await-of loop.
for-await-of loops (for the procedureName procedure) to break after they have finished iterating over their current backlogs of data.
for-await-of loops for all procedures to break after they have finished consuming their respective backlogs of data.
for-await-of loops for the procedureName procedure to break immediately and will reset the backpressure for that procedure to 0.
for-await-of loops for all procedures to break immediately and will reset the aggregate backpressure for all procedures to 0.
receiver. You can pass any JSON-compatible object as data.
This method doesn't return anything and doesn't throw or reject.
procedure (RPC) on the corresponding server side socket. You can pass any JSON-compatible object as data. This method returns a Promise. Note that there is a default timeout of 10 seconds to receive a response from the server. You can change this limit by setting ackTimeout when instantiating the client. If the client does not receive a response in time, the returned Promise will reject with a TimeoutError.
authKey option passed to the AGServer constructor).
This method returns a Promise which resolves on success and rejects with an Error on failure (e.g. if the token was invalid).
listener streams on the current socket.
eventName listener stream on the current socket. The aggregate backpressure represents the highest backpressure of all consumers for that listener.
receiver streams on the current socket.
procedure streams on the current socket.
channel streams on the current socket.
SocketCluster lets you interact with channels either directly through the socket or through AGChannel objects.
channelName. Do not expect a response from the server.
The channelName argument must be a string.
The data argument can be any JSON-compatible object/array or primitive.
channelName. Expect a response from the server.
The channelName argument must be a string.
The data argument can be any JSON-compatible object/array or primitive.
This method returns a Promise which will be rejected if the operation fails.
For example, it can be rejected if the MIDDLEWARE_INBOUND middleware blocks the action on the server side.
The promise will resolve once the server has processed the publish action.
options object in the form {waitForAuth: true, data: someCustomData} (all properties are optional) - If waitForAuth is true, the channel will wait for the socket to become authenticated before trying to subscribe to the server - These kinds of channels are called "private channels" - Note that in this case, "authenticated" means that the client socket has received a valid JWT authToken - Read about the server side socket.setAuthToken(tokenData) function here for more details. The data property can be used to pass data along with the subscription.
To consume a channel, it is recommended to use a for-await-of loop like this:
for await (
let data of socket.channel('myChannel')
) {
// Consume channel data...
}
Because the socket.subscribe(...) method returns an AGChannel instance, you can also consume it directly like this:
for await (
let data of socket.subscribe('myChannel')
) {
// Consume channel data...
}
Note that socket.subscribe(...) can be called multiple times for the same channel; if already subscribed, the method will only return the channel.
subscribe(channelName) again at a later time.
socket.subscribe(...) in that it will not try to subscribe to that channel.
The returned channel will be inactive initially.
You can call channel.subscribe() later to activate that channel when required.
for-await-of loops (for the channelName channel and all of its listeners) to break after they have finished iterating over their current backlogs of events.
closeChannel(channelName) except that it only closes the main channel (output) stream. Listener consumers on the channel will not be affected.
closeChannel(channelName) except that it only closes listener streams on the channel. The main channel output stream will not be affected.
To close specific listeners (by eventName) on a specific channel, it's recommended that you use the AGChannel API.
for-await-of loops for all channels (and all of their listeners) to break after they have finished consuming their respective backlogs of events.
for-await-of loops for the channelName channel (and all of its listeners) to break immediately. This will reset the backpressure for that channel (and all of its event listeners) to 0.
killChannel(channelName) except that it only kills the main channel (output) stream. Listener consumers on the channel will not be affected.
killChannel(channelName) except that it only kills listener streams on the channel. The main channel output stream will not be affected.
To kill specific listeners (by eventName) on a specific channel, it's recommended that you use the AGChannel API.
for-await-of loops for all channels (and all of of their listeners) to break immediately. This will reset the aggregate backpressure for all channels (and all of their event listeners) to 0.
killAllChannels() except that it only kills channel output streams; channel event listeners will not be affected. This will reset the aggregate backpressure for all channel output streams to 0.
killAllChannels() except that it only kills channel listener streams; channel output streams will not be affected. This will reset the aggregate backpressure for all channel listener streams to 0.
includePending is true, pending subscriptions will also be included in the list.
includePending is true, pending subscriptions will also be included in the list.
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.
id and backpressure property.
id and backpressure property.
for-await-of loop to break immediately.
id and backpressure property.
id and backpressure property.
for-await-of loop to break immediately.
id and backpressure property.
id and backpressure property.
id and backpressure property.
id and backpressure property.
for-await-of loop to break immediately.
for-await-of loop to break immediately.