# Class Channel

Channel class for Phoenix WebSocket communication

- Defined in channel.ts:17

##### Index

### Constructors

constructor

### Properties

socket
stateChangeRefs
topic

### Methods

join
leave
off
on
onClose
onError
onMessage
push

## Constructors

### constructor

- new Channel(topic: string, params: any, socket: Socket): Channel

  #### Parameters

  - topic: string
  - params: any
  - socket: Socket

  #### Returns Channel

  - Defined in channel.ts:58

## Properties

### `Internal` socket

socket: Socket

- Defined in channel.ts:46

### `Internal` stateChangeRefs

stateChangeRefs: string[]

- Defined in channel.ts:51

### `Internal` topic

topic: string

- Defined in channel.ts:56

## Methods

### join

- join(timeout?: number): Push

  Join the channel

  #### Parameters

  - timeout: number = ...

  #### Returns Push

  - Defined in channel.ts:150

### leave

- leave(timeout?: number): Push

  Leaves the channel

  Unsubscribes from server events, and instructs channel to terminate on server

  Triggers onClose() hooks

  To receive leave acknowledgements, use the `receive` hook to bind to the server ack, ie:

  #### Parameters

  - timeout: number = ...

  #### Returns Push

  #### Example

      channel.leave().receive("ok", () => alert("left!") )

  - Defined in channel.ts:278

### off

- off(event: string, ref?: number): void

  Unsubscribes off of channel events

  Use the ref returned from a channel.on() to unsubscribe one handler, or pass nothing for the ref to unsubscribe all handlers for the given event.

  #### Parameters

  - event: string
  - `Optional` ref: number

  #### Returns void

  #### Example

      // Unsubscribe the do_stuff handler
      const ref1 = channel.on("event", do_stuff)
      channel.off("event", ref1)

      // Unsubscribe all handlers from event
      channel.off("event")

  - Defined in channel.ts:218

### on

- on(
  event: string,
  callback: (payload: any, ref?: string, joinRef?: string) => void,
  ): number

  Subscribes on channel events

  Subscription returns a ref counter, which can be used later to unsubscribe the exact event listener

  #### Parameters

  - event: string
  - callback: (payload: any, ref?: string, joinRef?: string) => void

  #### Returns number

  #### Example

      const ref1 = channel.on("event", do_stuff)
      const ref2 = channel.on("event", do_other_stuff)
      channel.off("event", ref1)
      // Since unsubscription, do_stuff won't fire,
      // while do_other_stuff will keep firing on the "event"

  - Defined in channel.ts:194

### onClose

- onClose(
  callback: (payload?: any, ref?: string, joinRef?: string) => void,
  ): number

  Hook into channel close

  #### Parameters

  - callback: (payload?: any, ref?: string, joinRef?: string) => void

  #### Returns number

  - Defined in channel.ts:166

### onError

- onError(callback: (reason: any, ref?: string, joinRef?: string) => void): number

  Hook into channel errors

  #### Parameters

  - callback: (reason: any, ref?: string, joinRef?: string) => void

  #### Returns number

  - Defined in channel.ts:175

### onMessage

- onMessage(_event: string, payload: any, _ref?: string, _joinRef?: string): any

  Overridable message hook

  Receives all events for specialized message handling before dispatching to the channel callbacks.

  Must return the payload, modified or unmodified

  #### Parameters

  - _event: string
  - payload: any
  - `Optional`_ref: string
  - `Optional`_joinRef: string

  #### Returns any

  - Defined in channel.ts:313

### push

- push(event: string, payload?: any, timeout?: number): Push

  Sends a message `event` to phoenix with the payload `payload`. Phoenix receives this in the `handle_in(event, payload, socket)` function. if phoenix replies or it times out (default 10000ms), then optionally the reply can be received.

  #### Parameters

  - event: string
  - payload: any = {}
  - timeout: number = ...

  #### Returns Push

  #### Example

      channel.push("event")
        .receive("ok", payload => console.log("phoenix replied:", payload))
        .receive("error", err => console.log("phoenix errored", err))
        .receive("timeout", () => console.log("timed out pushing"))

  - Defined in channel.ts:247