# Class Socket

The Socket class.

For IE8 support use an ES5-shim (https://github.com/es-shims/es5-shim)

- Defined in socket.ts:252

##### Index

### Constructors

constructor

### Methods

channel
connect
connectionState
disconnect
endPointURL
getLongPollTransport
hasLogger
isConnected
log
makeRef
off
onClose
onError
onMessage
onOpen
ping
protocol
push
replaceTransport

## Constructors

### constructor

- new Socket(endPoint: string, opts?: SocketOptions): Socket

  Initializes the socket.

  #### Parameters

  - endPoint: string

    The string WebSocket endpoint, ie, `"ws://example.com/socket"`, `"wss://example.com"` `"/socket"` (inherited host & protocol)

  - opts: SocketOptions = {}

    Optional configuration

  #### Returns Socket

  - Defined in socket.ts:304

## Methods

### channel

- channel(topic: string, chanParams?: Record<string, any>): Channel

  Initiates a new channel for the given topic

  #### Parameters

  - topic: string
  - chanParams: Record<string, any> = {}

    Parameters for the channel

  #### Returns Channel

  - Defined in socket.ts:876

### connect

- connect(params?: Record<string, any>): void

  #### Parameters

  - `Optional` params: Record<string, any>

    The params to send when connecting, for example `{user_id: userToken}`

    Passing params to connect is deprecated; pass them in the Socket constructor instead: `new Socket("/socket", {params: {user_id: userToken}})`.

  #### Returns void

  - Defined in socket.ts:469

### connectionState

- connectionState(): string

  #### Returns string

  - Defined in socket.ts:823

### disconnect

- disconnect(callback?: () => void, code?: number, reason?: string): void

  Disconnects the socket

  See https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes for valid status codes.

  #### Parameters

  - `Optional` callback: () => void

    Callback which is called after socket is disconnected.

  - `Optional` code: number

    A status code for disconnection.

  - `Optional` reason: string

    A textual description of the reason to disconnect.

  #### Returns void

  - Defined in socket.ts:446

### endPointURL

- endPointURL(): string

  The fully qualified socket url

  #### Returns string

  - Defined in socket.ts:422

### getLongPollTransport

- getLongPollTransport(): TransportClass

  Returns the LongPoll transport reference

  #### Returns TransportClass

  - Defined in socket.ts:386

### hasLogger

- hasLogger(): boolean

  Returns true if a logger has been set on this socket.

  #### Returns boolean

  - Defined in socket.ts:500

### isConnected

- isConnected(): boolean

  #### Returns boolean

  - Defined in socket.ts:839

### log

- log(kind: string, msg: string, data?: any): void

  Logs the message. Override `this.logger` for specialized logging. noops by default

  #### Parameters

  - kind: string
  - msg: string
  - `Optional` data: any

  #### Returns void

  - Defined in socket.ts:493

### makeRef

- makeRef(): string

  Return the next message ref, accounting for overflows

  #### Returns string

  - Defined in socket.ts:904

### off

- off(refs: string[]): void

  Removes `onOpen`, `onClose`, `onError,` and `onMessage` registrations.

  #### Parameters

  - refs: string[]

    list of refs returned by calls to `onOpen`, `onClose`, `onError,` and `onMessage`

  #### Returns void

  - Defined in socket.ts:858

### onClose

- onClose(callback: (event: CloseEvent) => void): string

  Registers callbacks for connection close events

  #### Parameters

  - callback: (event: CloseEvent) => void

  #### Returns string

  - Defined in socket.ts:521

### onError

- onError(
  callback: (
  error: any,
  transport?: Transport,
  establishedConnections?: number,
  ) => void,
  ): string

  Registers callbacks for connection error events

  #### Parameters

  - callback: (error: any, transport?: Transport, establishedConnections?: number) => void

  #### Returns string

  #### Example

      socket.onError(function(error){ alert("An error occurred") })

  - Defined in socket.ts:534

### onMessage

- onMessage(callback: (message: Message) => void): string

  Registers callbacks for connection message events

  #### Parameters

  - callback: (message: Message) => void

  #### Returns string

  - Defined in socket.ts:544

### onOpen

- onOpen(callback: () => void): string

  Registers callbacks for connection open events

  #### Parameters

  - callback: () => void

  #### Returns string

  #### Example

      socket.onOpen(function(){ console.info("the socket was opened") })

  - Defined in socket.ts:511

### ping

- ping(callback: (rtt: number) => void): boolean

  Pings the server and invokes the callback with the RTT in milliseconds

  #### Parameters

  - callback: (rtt: number) => void

    Returns true if the ping was pushed or false if unable to be pushed.

  #### Returns boolean

  - Defined in socket.ts:556

### protocol

- protocol(): string

  Returns the socket protocol

  #### Returns string

  - Defined in socket.ts:413

### push

- push(data: Message): void

  #### Parameters

  - data: Message

  #### Returns void

  - Defined in socket.ts:885

### replaceTransport

- replaceTransport(newTransport: Transport): void

  Disconnects and replaces the active transport

  #### Parameters

  - newTransport: Transport

    The new transport class to instantiate

  #### Returns void

  - Defined in socket.ts:396