-- import "github.com/anikhasibul/push"
Package push gives you the ability to use push and pull mechanism for notification or message via websocket or even http client.
func DeleteSession(sessionID interface{})DeleteSession deletes the given session from memory. It's safe to delete a non-existent session.
func Exists(sessionID interface{}) boolExists returns true if the given session exists.
type Client struct {
}Client holds the methods for a perticular client.
func (c *Client) Close()Close closes a client channel/connection
func (c *Client) DeleteSelf()DeleteSelf deletes the current client from the current session.
func (c *Client) Key() interface{}Key returns the current clientID/name/key.
func (c *Client) KeyString() stringKeyString returns the current clientID/name/key in string type.
func (c *Client) Pull() (content interface{}, err error)Pull pulls a message for the current client.
var (
userID := 123446555
clID := "device_mobile_5445"
)
s := NewSession(userID)
c := s.NewClient(clID)
defer c.DeleteSelf()
msg, err := c.Pull()
if err != nil {
panic(err)
}
fmt.Println(msg)
func (c *Client) PullChan() (ClientChan, error)PullChan returns a channel for receiving messages for the current client.
var (
userID := 123446555
clID := "device_mobile_5645"
)
s := NewSession(userID)
c := s.NewClient(clID)
defer c.DeleteSelf()
ch, err := c.PullChan()
if err != nil {
panic(err)
}
msg := <-ch
fmt.Println(msg)
Exclusively usable with websockets
type ClientChan chan interface{}ClientChan holds a chan of interface type to provide type flexibility on pushed message
type Session struct {
// `MaxChannelBuffer` means the maximum buffered message on a client channel. `make(chan interface{},MaxChannelBuffer)`.
// Default value is 10
MaxChannelBuffer int
}Session holds the methods for push and pull mechanism
func NewSession(sessionID interface{}) *SessionNewSession returns a client session.
A single user (sessionID) can use multiple devices (clientID). That's why the clientID should be unique for each device/client/connection.
`sessionID` means the userID or a groupID. Once a `Session` receives a message, it pushes the message to all registered client for this session.
func (s *Session) Clients() []interface{}Clients returns the keys/IDs/names of active clients on current session.
func (s *Session) DeleteClient(clientID interface{})DeleteClient deletes the given client from the current session. It's safe to delete a non-existent client.
func (s *Session) DeleteSelf()DeleteSelf deletes the current session from memory.
func (s *Session) Exists(clientID interface{}) boolExists returns true if the given client exists.
func (s *Session) Len() intLen returns the length/count of active clients on current session.
func (s *Session) NewClient(clientID interface{}) *ClientNewClient returns a Client.
It creates a new client if the given clientID does not exist.
It returns the existing client for the given clientID if it already exists.
it panics if the given clientID is nil.
A single user (sessionID) can use multiple devices (clientID). That's why the clientID should be unique for each device/client/connection.
func (s *Session) Push(message interface{})Push sends a message to all connected clients on the given session.
s := push.NewClient(userID, nil,0)
s.Push("Hello world!")