pusher

package module
v0.0.0-...-d33b2ac Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2017 License: MIT Imports: 9 Imported by: 0

README

pusher-websocket-go

Go client (WebSocket) library for Pusher.

Status: Not currently recommended for production use. The code connects, receives messages, and reconnects on failure, but there are no tests, interfaces may change, and there are a few missing features (see todo list below).

Usage

Create a new client. It establishes a WebSocket connection and handles automatic reconnection.

client := pusher.New("<key>")

Subscribe to one or more Pusher channels. There is no need to wait for the client to connect before subscribing.

channel := pusher.Subscribe("<channel>")

To bind to events:

channel.bind("my-event", func(data interface{}){
  fmt.Println(data)
})

client.bind(func (channel string, event string, data interface{}) {
  fmt.Println(channel, event, data)
})

TODO

  • Read close code, adjust reconnect behaviour
  • Expose client connection state changes

Documentation

Overview

Package pusher provides a client library for Pusher. It connects to the WebSocket interface, allows subscribing to channels, and receiving events.

Index

Constants

This section is empty.

Variables

View Source
var Debug = false

Functions

This section is empty.

Types

type Channel

type Channel struct {
	Subscribed bool
	Name       string
	// contains filtered or unexported fields
}

func (*Channel) Bind

func (self *Channel) Bind(event string, callback EventHandler)

func (*Channel) Trigger

func (self *Channel) Trigger(event string, data interface{})

type Client

type Client struct {
	ClientConfig

	Connected bool
	Channels  []*Channel
	UserData  Member
	Debug     bool
	// contains filtered or unexported fields
}

Client responsibilities:

* Connecting (via Connection) * Reconnecting on disconnect * Decoding and encoding events * Managing channel subscriptions

func New

func New(key string) *Client

New creates a new Pusher client with given Pusher application key

func NewWithConfig

func NewWithConfig(c ClientConfig) *Client

NewWithConfig allows creating a new Pusher client which connects to a custom endpoint

func (*Client) BindGlobal

func (self *Client) BindGlobal(callback func(string, string, interface{}))

func (*Client) Disconnect

func (self *Client) Disconnect()

func (*Client) Subscribe

func (self *Client) Subscribe(channel string) (ch *Channel)

Subscribe subscribes the client to the channel

func (*Client) Unsubscribe

func (self *Client) Unsubscribe(channel string)

UnSubscribe unsubscribes the client from the channel

type ClientConfig

type ClientConfig struct {
	Scheme       string
	Host         string
	Port         string
	Key          string
	Secret       string
	AuthEndpoint string
}

type Event

type Event struct {
	Name    string `json:"event"`
	Channel string `json:"channel"`
	Data    string `json:"data"`
}

type EventHandler

type EventHandler func(data interface{})

type Member

type Member struct {
	UserId   string            `json:"user_id"`
	UserInfo map[string]string `json:"user_info,omitempty"`
}

type Members

type Members struct {
	Count   int
	Members []Member
	Me      Member
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL