Inherits from
Properties
socket.CONNECTING, socket.OPEN or socket.CLOSED.
socket.AUTHENTICATED or socket.UNAUTHENTICATED.
Events
error property
socket.send(...). The object produced by the listener will have a message property.
agServer.listener('connection') listener, then the socket-level 'connect' event will already have occurred by that point so you won't be able to capture it on the socket. The 'connect' event can only be handled from a socket which was captured from an agServer.listener('handshake') listener instead.
The object produced by the listener will have an id, pingTimeout and isAuthenticated property. If an authentication error occured during the socket handshake phase, the event object will also have an authError property.
'connectAbort' event will be triggered instead. The object produced by the listener will have a code and reason property.
socket.state was 'connecting').
Note that the 'connectAbort' event can only be triggered during the socket's handshake phase before the server's 'connection' event is triggered.
The object produced by the listener will have a code and reason property.
'disconnect' and 'connectAbort' events.
The object produced by the listener will have a code and reason property.
channel and subscriptionOptions property.
channel property.
'badSocketAuthToken' event directly on the server instance instead.
The object produced by the listener will have an authError and signedAuthToken property.
authToken property.
oldAuthToken property.
authState changes (e.g. transitions between authenticated and unauthenticated states).
The object produced by the listener will have an oldAuthState and newAuthState property.
message property.
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 client 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.
Authenticate the current socket's client by providing it some token data which you can use to validate their identity and/or access rights. Generally, you should call this method in response to a successful login attempt by the client (I.e. The client has provided a username and password combination which exists in your database).
When you call this method, a JWT (token) containing the specified data will be created and attached to both the current server socket and the client socket on the other side of the connection. This method can be called multiple times to update the token with new data - Avoid modifying the socket.authToken property directly because otherwise, the change will not propagate to the client.
In the simplest case, the data argument to this method would be an object in the form {username: 'bob123'}.
For a more complex use case, you could also store a list of authorized channel names inside the token (that way, you will be able to check access privileges directly from the socket's token instead of having to read the database every time).
The optional options argument is an Object which can be used to modify the token's behavior - Valid properties include any
option accepted by the node-jsonwebtoken library's sign method.
See the list of options here.
Note that if you use a different algorithm than the default 'HS256', you may need to provide an authPrivateKey and authPublicKey instead of the default authKey.
See authKey, authPrivateKey and authPublicKey options in AGServer constructor here.
Using auth tokens has the following advantages:
- The user doesn't need to login again if their socket loses its connection - Works well with SocketCluster's auto-reconnect feature.
- An auth token is shared between sockets across multiple tabs in a browser (under the same domain name). If a user logs in on one tab, they will automatically be authenticated on any other tab that they have open. Tokens therefore allow you to attach a user's identity to multiple sockets.
eventName listener stream on the current socket. 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.
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.