These are exposed by require('socket.io-client')
or is defined as io
in a browser:
version
(String) defined the socket.io versionprotocol
(Number): defined the protocol supportedtransports
(Object): map of available transportsj
(Array): internal array used to track jsonp callbackssockets
(Function): internal function defined in socket.jsutil
(Object): internal utility functions for inheritance and more. Defined in util.jsJSON
(Object): internal json utilities based on JSON2.js. Defined in json.jsparser
(Object): internal object for encoding data. Defined in parser.jsEventEmitter
(Object): EventEmitter Constructor. Defined in events.jsSocketNamespace
: (Function): Internal function for namespacing different sockets. Defined in namespace.jsTransport
(Function): Internal function for shared features among transports. Defined in namespace.js
connect
- Connects and returns a Client object.
- Parameters
Host
: String containing the full URI (e.g., "http://www.some_server.com")Options
: Map of connection options. View socket.options property to get a full list of options.
- Returns
Client
Object returned from from connect. This object is where you setup socket.io events and listeners. Inherits from EventEmitter.
connecting
- Called when a connection is being attempted
- Arguments
Transport Name
: Name name of the transport the connection was established with (e.g., 'websocket')
connect
- Called when a socket connection is established
disconnect
- Called when a the established connection has been disconnected
- Arguments
Reason
: The reason the disconnect occurred (if known)
Socket
: Socket class instancename
(Function): internal namespace identifierflags
(Object):internal namespace propertyjson
(Object): internal namespace Flag instanceackPackets
(Object): internal namespace propertyacks
(Object): internal namespace property$events
(Object): internal mapping of all registered events, included events registered with the "on" method. Never use directly. Inherited from EventEmitter
on
:- Registers a new event. Inherited from EventEmitter
- Parameters
Name
: (String) Name of eventFunction
: (Function) The function to be executed when event is triggered
- Returns
Client
for chaining
once
- Registers a new event. Listened for only a single time. Inherited from EventEmitter
- Parameters
Name
: (String) Name of eventFunction
: (Function) The function to be executed when event is triggered
- Returns
Client
for chaining
removeListener
- Removes a registered event. Inherited from EventEmitter
- Parameters
Name
: (String) Name of event to removeFunction
: (Function) The function signature to remove
- Returns
Client
for chaining
removeAllListeners
- Removes all registered listeners for a registered event. Inherited from EventEmitter
- Parameters
Name
: (String) Name of event to remove
- Returns
Client
for chaining
listeners
- Gets all listeners for a registered event. Inherited from EventEmitter
- Parameters
Name
: (String) Name of event to get to registered listeners for
- Returns
Events
(Array) All events for a given name
emit
- Fire an event. Inherited from EventEmitter
- Parameters
Name
: (String) Name of event to fireArgs
: Remaining arguments passed to the receiving function
- Returns
Success
(Boolean)
Socket the the client uses to communicate to the socket.io server instance. Rarely will you want to work directly with this object but instead use the client instance. Exceptions to this are forcing a disconnect (client.socket.disconnect()), etc . Inherits from EventEmitter.
options
(Object): All socket optionssecure
: (boolean) whether this is a secure (HTTPS or WSS) connectionport
: (number) port number for connection. Will use 443 or 80 by default (depending on secure options)query
: (string) Get like parameters to pass with the connection (e.g., "param1=foo¶m2=bar")document
: Pointer to the dom level document, used insert the client side script in domresource
: The url resource that it connects to. Default is 'socket.io'. You should not change this unless you really know what you are doing.transports
: Array of strings for the avaialable transportsconnect timeout
: How long in ms until it gives up connecting with a transport. Default: 10000 (ms)try multiple transports
: Boolean for whether it will attempt to try more than onereconnect
: Whether it will attempt to reconnect after being disconnected. Default: true,reconnection delay
: The starting delay for reconnecting. Socket.IO will exponentially increase the day for reconnecting after each failed attempt. Default: 500 (ms)reconnection limit
: How many times it will exponentially adjust the reconnection delay. Default: Infinity (ecmascript static value).max reconnection attempts
: How many times it will try to reconnect. Default: 10sync disconnect on unload
: Will attempt to synchronize the disconnect before disconnecting. Default: falseauto connect
: Whether the socket should automatically connect as part of this Connect call. Default: trueflash policy port
: Default: 10843manualFlush
: Whether the transport buffer should be flushed manually or not. Default: false
connected
(Boolean): request that originated the Socketopen
(Boolean): whether the transport has been upgradedconnecting
(Boolean): opening|open|closing|closedreconnecting
(Boolean): transport referencenamespaces
(Object): internal object containing all transport namespaces
connect
:- Connects using the available transports. This is called for you using the top level connect.
- Parameters
fn
: (Function) A callback upon a successful network connections
- Returns
Socket
for chaining
packet
- Sends a message. Emit takes care of this on behalf of you.
- Returns
Socket
for chaining
flushBuffer
- Manually flushes the buffer
disconnect
- Disconnects the current connected transport
- Returns
Socket
for chaining
var server = http.createServer(app); // Create server (app assumes express instance)
// Top Level Object
var topLevel = require('socket.io');
// Manager instance, usually combines with the call above
var io = io.listen(server, {
origins: '*:*'
});
These are exposed by require('socket.io')
version
(String) defined the socket.io versionprotocol
(Number): defined the protocol supportedclientVersion
(String): the client version servedManager
(Object): manager constructor (which is returned by listen)Transport
(Object): transport constructorSocket
(Object): socket constructorStatic
(Object): static constructorStore
(Object): generic store constructorMemoryStore
(Object): memory store constructor (single isntance only)RedisStore
: (Object): redis store constructor (used for horizontally scaling)parser
(Function): internal object for encoding data. Defined in parser.js
listen
- Creates or piggy backs an an existing http server to handle socket.io events
- Parameters
Server
: An HTTPServer instance or a port to create a new instance usingOptions
: A JSON object of options for the manager (see manager docs for list and description)Callback
: A function that is called after the server instance is started (only valid if you passed a number for Server param)
- Returns
Manager Instance
Created and returned by the listen method of the top level object.
It accepts the following options (pass to the connect method of the top level object):
- origins: ':'
- log: true
- store: new MemoryStore
- logger: new Logger
- static: new Static(this)
- heartbeats: true
- resource: '/socket.io'
- transports: defaultTransports
- authorization: false
- blacklist: ['disconnect']
- 'log level': 3
- 'log colors': tty.isatty(process.stdout.fd)
- 'close timeout': 60
- 'heartbeat interval': 25
- 'heartbeat timeout': 60
- 'polling duration': 20
- 'flash policy server': true
- 'flash policy port': 10843
- 'destroy upgrade': true
- 'destroy buffer size': 10E7
- 'browser client': true
- 'browser client cache': true
- 'browser client minification': false
- 'browser client etag': false
- 'browser client expires': 315360000
- 'browser client gzip': false
- 'browser client handler': false
- 'client store expiration': 15
- 'match origin protocol': false
store
(Store Instance) Returns the store in use (use to get only)log
(Logger Instance) Instance of logger (use to get only)static
(Static Instance) Instance of static (use to get only)
get
- Used to get a setting/option using the provided key
- Parameters
key
: (String)
- Returns `value for options using provided key
set
- Used to set a setting/option
- Parameters
key
: (String)value
: (Any)
- Returns Manger Instance for chaining
enable
- Enables (sets to true) the settings/option using the provided key
- Parameters
key
: (String)
- Returns Manger Instance for chaining
disable
- Disables (sets to false) the settings/option using the provided key
- Parameters
key
: (String)
- Returns Manger Instance for chaining
enabled
- Checks whether the setting/option is enabled (equals true) using the provided key
- Parameters
key
: (String)
- Returns (Boolean)
disable
- Checks whether the setting/option is disabled (equals false) using the provided key
- Parameters
key
: (String)
- Returns (Boolean)
configure
- Used to configure settings based on the environment (NODE_ENV). If it provided environment is true then it calls the provided callback.
- Parameters
env
: (String) Name of environmentfn
: (Function) Callback if the environment is considered true
- Returns Manger Instance for chaining
connect
- Called when a socket is connected
Socket is the objects that allows you to setup events and emit events back to the client. You get a socket instance from the Managers connection event.
json
: Specialbroadcast
: Special property that when used prior to an emit will broadcast event to everyone except the socket that originates the event.volatile
: Special property that when included prior to an emit will cause the emit to be volatile. Volatile messages are not assured to get to the client as a normal emit is.
in
- Puts socket.io into a specific room namespace
- Parameters
Room
: A strong for the user name you would like to use
- Returns
Socket Instance in specified namespace
emit
- Fires off a new event message
- Parameters
Name
: (String)args
: An arbritrary number of arguments can be added that will be given on the received end.acknowledgement
: (function) A function at the end of the parameters will be treated an acknowledgement function. It will be called if the receiving end responds
- Returns
packet
send
- Fires off a simple message that can be heard on the other end by listening the the message event.
- Parameters
data
: (anything) data you want to sendacknowledgement
(function) An acknowledgment function that will be called if the receiving end responds
- Returns
packet