Edit Page

SailsSocket methods

This section describes the methods available on each SailsSocket instance. Most of these methods can be called before the socket even connects to the server. In the case of request methods like .get() and .request(), calls will be queued up until the socket connects, at which time they will be executed in order.

Basic methods

#

The most common methods you will use with a SailsSocket instance are documented in the main Socket Client reference section. These include .get(), .put(), .post(), .delete(), .request(), .on() and .off().

Advanced methods

#

In addition to the basic communication and event-listening methods, each SailsSocket instance (including io.socket) exposes several methods for dealing with server connections.

.isConnected()
#

Determines whether the SailsSocket instance is currently connected to a server; returns true if a connection has been established.

io.socket.isConnected();
.isConnecting()
#

Determines whether the SailsSocket instance is currently in the process of connecting to a server; returns true if a connection is being attempted.

io.socket.isConnecting();
.mightBeAboutToAutoConnect()
#

Detects when the SailsSocket instance has already loaded but is not yet fully configured or has not attempted to autoconnect.

The sails.io.js library waits one tick of the event loop before checking whether autoConnect is enabled and, if so, trying to connect. This allows you to configure the SailsSocket instance (for example, by setting io.sails.url) before an attempt is made to estabilish a connection. The mightBeAboutToAutoConnect() method returns true in the situation where sails.io.js has loaded, but the requisite tick of the event loop has not yet elapsed.

io.socket.mightBeAboutToAutoConnect();
.disconnect()
#

Disconnects a SailsSocket instance from the server; throws an error if the socket is already disconnected.

io.socket.disconnect();
.reconnect()
#

Reconnects a SailsSocket instance to a server after it's been disconnected (either involuntarily or via a call to .disconnect()). The instance connects using its currently configured properties. .reconnect() throws an error if the socket is already connected to a server.

io.socket.reconnect();

When an instance is in a disconnected state, its properties may be changed. This means that an instance that has been disconnected from one server can be reconnected to another without losing its event bindings or queued requests.

.removeAllListeners()
#

Stops listening for any server-related events on a SailsSocket instance, including connect and disconnect.

io.socket.removeAllListeners();

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

Reference