socket

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

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

Go to latest
Published: Mar 20, 2024 License: MIT Imports: 13 Imported by: 2

README

socket

Build Status | codecov | Go Report Card | GoDoc

A service that allows you to broadcast Publish and Listen messages to/from the server.

If i miss something or you have something interesting, please be part of this project. Let me know! My contact is at the end.

How it works ?

Flow example:

  • CLIENT: creates a gateway server
  • CLIENT: subscribe a topic/channel on the server
  • SERVER: register the client on the topic/channel with the gateway to call it back later
  • CLIENT: send a message to a topic/channel
  • SERVER: receives the message and broadcast the message to all registered clients
  • CLIENT: all clients receive the message sent
  • CLIENT: unsubscribe the topic/channel on the server

Usage

This examples are available in the project at socket/examples

Server
func main() {
	// server
	server, err := socket.NewServer()
	if err != nil {
		panic(err)
	}

	if err := server.Start(); err != nil {
		panic(err)
	}
}
Client
func main() {
	client, err := socket.NewClient()
	if err != nil {
		panic(err)
	}

	if err := client.Start(); err != nil {
		panic(err)
	}

	if err := client.Subscribe("topic_1", "channel_1"); err != nil {
		panic(err)
	}

	client.Listen("topic_1", "channel_1", func(message []byte) error {
		fmt.Printf("\nreceived on listener the message %s", string(message))
		return nil
	})

	if err := client.Publish("topic_1", "channel_1", []byte("hello, this is a test message")); err != nil {
		panic(err)
	}

	// Unsubscribe
	//if err := client.Unsubscribe("topic_1", "channel_1"); err != nil {
	//	panic(err)
	//}

	client.Wait()
}

Dependecy Management

Dependency

Project dependencies are managed using Dep. Read more about Dep.

  • Get dependency manager: go get github.com/joaosoft/dependency
  • Install dependencies: dependency get
Go
go get github.com/joaosoft/socket

Known issues

Follow me at

Facebook: https://www.facebook.com/joaosoft

LinkedIn: https://www.linkedin.com/in/jo%C3%A3o-ribeiro-b2775438/

If you have something to add, please let me know joaosoft@gmail.com

Documentation

Index

Constants

View Source
const (
	HeaderGatewayKey = "Gateway"
)

Variables

This section is empty.

Functions

func EncodeString

func EncodeString(s string) string

func Exists

func Exists(file string) bool

func GetEnv

func GetEnv() string

func ReadFile

func ReadFile(file string, obj interface{}) ([]byte, error)

func ReadFileLines

func ReadFileLines(file string) ([]string, error)

func WriteFile

func WriteFile(file string, obj interface{}) error

Types

type AppConfig

type AppConfig struct {
	Socket SocketConfig `json:"socket"`
}

AppConfig ...

func NewConfig

func NewConfig() (*AppConfig, manager.IConfig, error)

NewConfig ...

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(options ...SocketClientOption) (*Client, error)

NewClient ...

func (*Client) Config

func (c *Client) Config() *ClientConfig

func (*Client) Forget

func (c *Client) Forget(topic, channel string, handler MessageHandler)

Forget ...

func (*Client) Listen

func (c *Client) Listen(topic, channel string, handler MessageHandler)

Listen ...

func (*Client) Publish

func (c *Client) Publish(topic, channel string, message []byte) error

Publish ...

func (*Client) Reconfigure

func (c *Client) Reconfigure(options ...SocketClientOption)

Reconfigure ...

func (*Client) Start

func (c *Client) Start(waitGroup ...*sync.WaitGroup) error

Start ...

func (*Client) Started

func (c *Client) Started() bool

func (*Client) Stop

func (c *Client) Stop(waitGroup ...*sync.WaitGroup) error

Stop ...

func (*Client) Subscribe

func (c *Client) Subscribe(topic, channel string) error

Subscribe ...

func (*Client) Unsubscribe

func (c *Client) Unsubscribe(topic, channel string) error

Unsubscribe ...

func (*Client) Wait

func (c *Client) Wait()

Forget ...

type ClientConfig

type ClientConfig struct {
	ServerAddress string `json:"server_address"`
	*web.ClientConfig
}

ClientConfig ...

type MessageHandler

type MessageHandler func(message []byte) error

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(options ...SocketServerOption) (*Server, error)

NewServer ...

func (*Server) Config

func (s *Server) Config() *ServerConfig

func (*Server) Reconfigure

func (s *Server) Reconfigure(options ...SocketServerOption)

Reconfigure ...

func (*Server) Start

func (s *Server) Start(waitGroup ...*sync.WaitGroup) error

Start ...

func (*Server) Started

func (s *Server) Started() bool

func (*Server) Stop

func (s *Server) Stop(waitGroup ...*sync.WaitGroup) error

Stop ...

type ServerConfig

type ServerConfig struct {
	*web.ServerConfig
}

ServerConfig ...

type SocketClientOption

type SocketClientOption func(client *Client)

SocketClientOption ...

func WithClientConfiguration

func WithClientConfiguration(config *ClientConfig) SocketClientOption

WithClientConfiguration ...

func WithClientLogLevel

func WithClientLogLevel(level logger.Level) SocketClientOption

WithClientLogLevel ...

func WithClientLogger

func WithClientLogger(logger logger.ILogger) SocketClientOption

WithClientLogger ...

func WithClientManager

func WithClientManager(mgr *manager.Manager) SocketClientOption

WithClientManager ...

type SocketConfig

type SocketConfig struct {
	Server *ServerConfig `json:"server"`
	Client *ClientConfig `json:"client"`
}

SocketConfig ...

type SocketServerOption

type SocketServerOption func(server *Server)

SocketServerOption ...

func WithServerConfiguration

func WithServerConfiguration(config *ServerConfig) SocketServerOption

WithServerConfiguration ...

func WithServerLogLevel

func WithServerLogLevel(level logger.Level) SocketServerOption

WithServerLogLevel ...

func WithServerLogger

func WithServerLogger(logger logger.ILogger) SocketServerOption

WithServerLogger ...

func WithServerManager

func WithServerManager(mgr *manager.Manager) SocketServerOption

WithServerManager ...

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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