phoenix
    Preparing search index...

    Class Channel

    Channel class for Phoenix WebSocket communication

    Index

    Constructors

    Properties

    socket: Socket
    stateChangeRefs: string[]
    topic: string

    Methods

    • Join the channel

      Parameters

      • timeout: number = ...

      Returns 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

      channel.leave().receive("ok", () => alert("left!") )
      
    • 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
      • Optionalref: number

      Returns void

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

      // Unsubscribe all handlers from event
      channel.off("event")
    • 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

      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"
    • Hook into channel close

      Parameters

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

      Returns number

    • Hook into channel errors

      Parameters

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

      Returns number

    • 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

    • 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

      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"))