API - SCServer
This instance is returned from the call to worker.getSCServer(). It represents your real-time (WebSocket) server object which you can use to listen for incoming real-time socket connections. This object inherits from engine.io's Server object so you have access to all the properties and methods of an engine.io server but with some additions.
Inherits from:
Properties:
exchange | A top-level scBroker client which lets you publish and manipulate data within your brokers. (See API section on Exchange object for details). |
clients | An object which holds all fully connected clients in the current worker (only those who have completed the handshake). Keys are socket IDs and the values are SCSocket instances. |
clientsCount | The number of clients currently connected to this server. |
pendingClients [since v9.1.0] |
An object which holds all pending clients in the current worker.
Keys are socket IDs and the values are SCSocket 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 [since v9.1.0] | 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). |
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 provide a callback to socket.emit() on the client (it will expect a response from the server). |
Events:
'error' | This gets triggered when fatal error occurs on this worker. |
'notice' | A notice carries potentially useful information but isn't quite an error. |
'handshake' | Emitted as soon as a new SCSocket object is created on the server - This occurs at the beginning of the client handshake, before the 'connection' event. The argument passed to the listener is the socket object which is performing the handshake. You should not try to send events to the socket while it is in this state. |
'connectionAbort' [since v9.1.0] | Emitted whenever a socket becomes disconnected during the handshake phase. The listener to this event receives a socket (SCSocket) object as argument. |
'connection' | Emitted whenever a new socket connection is established with the server (and the handshake has completed). The listener to this event receives a socket (SCSocket) object as argument which can be used to interact with that client. The second argument to the handler is the socket connection status object. |
'disconnection' |
Emitted whenever a connected socket becomes disconnected (after the handshake phase). The listener to this event
receives a socket (SCSocket) object as argument. Note that if the socket connection was not fully established (e.g. during the SC handshake phase), then the
'connectionAbort' event will be triggered instead.
|
'closure' [since v9.1.1] |
Emitted whenever a connected socket becomes disconnected (at any stage of the handshake/connection cycle). The listener to this event
receives a socket (SCSocket) object as argument. Note that this event is a catch-all for both 'disconnection' and 'connectionAbort' events.
|
'subscription' | Emitted whenever a socket connection which is attached to the server becomes subscribed to a channel. The listener to this event receives a socket (SCSocket) object as the first argument. The second argument is the channelName. The third argument is the channelOptions object. |
'unsubscription' | Emitted whenever a socket connection which is attached to the server becomes unsubscribed from a channel. The listener to this event receives a socket (SCSocket) object as the first argument. The second argument is the channelName. |
'authentication' | Emitted whenever a socket connection which is attached to the server becomes authenticated. The listener to this event receives a socket (SCSocket) object as the first argument. The second argument is the authToken object. |
'deauthentication' | Emitted whenever a socket connection which is attached to the server becomes deauthenticated. The listener to this event receives a socket (SCSocket) object as the first argument. The second argument is the old authToken object (before the deauthentication took place). |
'authenticationStateChange' |
Triggers whenever the authState of a socket which is attached to the server changes (e.g. transitions between authenticated and unauthenticated states).
|
'badSocketAuthToken' |
Emitted when a client which is attached to the server tries to authenticate itself with an invalid (or expired) token.
The first argument passed to the handler is the socket object which failed authentication. The second argument is
an object with the properties authError and signedAuthToken .
The authError is an error object and the signedAuthToken is the auth token which failed the verification step.
|
'ready' | Emitted when the server is ready to accept connections. |
Methods:
setCodecEngine(codecEngine) |
Lets you set a custom codec engine. This allows you to specify how data gets encoded before being sent over the wire and how it gets decoded once it reaches the other side.
The codecEngine must be an object which exposes an |
|||||||||||||||||||||
close() |
Close the server and terminate all clients. |
|||||||||||||||||||||
addMiddleware(type, middlewareFn) |
Lets you add middleware functions which can be used to manage client access control to various features of the SCServer. 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 to scServer.exchange.publish() do not.
Example: |
|||||||||||||||||||||
removeMiddleware(type, middlewareFn) |
Lets you remove middleware functions previously added with the addMiddleware() method. |