gosf

package module
v0.0.0-...-237aea4 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2020 License: MIT Imports: 16 Imported by: 7

README

Go Socket.IO Framework (GOSF)

Go Socket.IO Framework or GOSF is an easy-to-use framework for developing Socket.IO API's in Google's Go language (GoLang).

Support ongoing development efforts with a transfer to BTC wallet 13pkXi8bW5bKjWewQYtv36CUgwhgQEn7eA

Supports Socket.IO Version 2

An example server to help you get started can be found at github.com/ambelovsky/gosf-sample-app.

For an in-depth look at the API Framework, check us out at gosf.io.

Get It

go get -u "github.com/ambelovsky/gosf"

Features

  • Socket.IO v2 Support
  • Request/Response Acknowledgement-Based Cycle
  • Broadcast/Room Support
  • Microservices Using Socket.IO
  • Plugable Architecture
  • App, Client, and Request Contexts
  • Standardized Message Format

Quick Start

The following sample will start a server that responds on an "echo" endpoint and return the same message received from the client back to the client.

Server
package main

import (
  f "github.com/ambelovsky/gosf"
)

func init() {
  // Listen on an endpoint
  f.Listen("echo", func(client *f.Client, request *f.Request) *f.Message {
    return f.NewSuccessMessage(request.Message.Text)
  })
}

func main() {
  // Start the server using a basic configuration
  f.Startup(map[string]interface{}{
    "port": 9999})
}
Client
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.slim.js"></script>
<script>
  var socket = io.connect('ws://localhost:9999', { transports: ['websocket'] });

  socket.emit('echo', { text: 'Hello world.' }, function(response) {
    console.log(response);
  });
</script>

Learn More

Discover more about GOSF with the complete documentation at gosf.io.

Documenting Your API

While you're building your API, take some time to build the documentation too! Check out github.com/ambelovsky/go-api-docs for an easy-to-use documentation system built using the slate theme.

Original Author

Aaron Belovsky is a senior technologist, avid open source contributor, and author of GOSF.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SupportedPlatforms []string

SupportedPlatforms is an array of OS platform names that this framework works well on

Functions

func ArrayContainsString

func ArrayContainsString(array []string, value string) bool

ArrayContainsString checks to see if a value exists in the array

func Broadcast

func Broadcast(room string, endpoint string, message *Message)

Broadcast sends a message to connected clients joined to the same room

func DeregisterMicroservice

func DeregisterMicroservice(name string)

DeregisterMicroservice disconnects and removes a microservice from App.Microservices

func GetConfig

func GetConfig(name string) interface{}

GetConfig returns a configuration from App.Config

func Listen

func Listen(endpoint string, callback func(client *Client, request *Request) *Message)

Listen creates a listener on an endpoint

func LoadConfig

func LoadConfig(name string, path string)

LoadConfig loads a JSON configuration file into the global Config map

func MapToStruct

func MapToStruct(input map[string]interface{}, output interface{}) error

MapToStruct converts the given map[string]interface{} into a struct

func OnAfterBroadcast

func OnAfterBroadcast(callback func(endpoint string, room string, response *Message))

OnAfterBroadcast registers an event handler that fires after a global broadcast is sent to all clients

func OnAfterClientBroadcast

func OnAfterClientBroadcast(callback func(client *Client, endpoint string, room string, response *Message))

OnAfterClientBroadcast registers an event handler that fires after a client broadcast is sent to other clients

func OnAfterRequest

func OnAfterRequest(callback func(client *Client, request *Request, response *Message))

OnAfterRequest registers an event handler that fires after a request is processed by the controller

func OnAfterResponse

func OnAfterResponse(callback func(client *Client, request *Request, response *Message))

OnAfterResponse registers an event handler that fires after a response is processed by the controller

func OnBeforeBroadcast

func OnBeforeBroadcast(callback func(endpoint string, room string, response *Message))

OnBeforeBroadcast registers an event handler that fires before a global broadcast is sent to all clients

func OnBeforeClientBroadcast

func OnBeforeClientBroadcast(callback func(client *Client, endpoint string, room string, response *Message))

OnBeforeClientBroadcast registers an event handler that fires before a client broadcast is sent to other clients

func OnBeforeRequest

func OnBeforeRequest(callback func(client *Client, request *Request))

OnBeforeRequest registers an event handler that fires before a request is processed by the controller

func OnBeforeResponse

func OnBeforeResponse(callback func(client *Client, request *Request, response *Message))

OnBeforeResponse registers an event handler that fires before a response is processed by the controller

func OnConnect

func OnConnect(callback func(client *Client, request *Request))

OnConnect registers an event handler that fires when a client connects

func OnDisconnect

func OnDisconnect(callback func(client *Client, request *Request))

OnDisconnect registers an event handler that fires when a client disconnects

func RegisterMicroservice

func RegisterMicroservice(name string, host string, port int, secure bool) error

RegisterMicroservice configures, automatically connects, and adds the microservice to App.Microservices

func RegisterPlugin

func RegisterPlugin(plugin Plugin)

RegisterPlugin is used by the plugin to register itself for later activation

func Shutdown

func Shutdown()

Shutdown cleanly terminates the framework and its plugins

func Startup

func Startup(config map[string]interface{})

Startup activates the framework and starts the server

func StructToMap

func StructToMap(input interface{}) map[string]interface{}

StructToMap converts the given structure into a map[string]interface{}

Types

type AppSettings

type AppSettings struct {
	Env           map[string]string
	Config        map[string]interface{}
	Microservices map[string]*Microservice
}

AppSettings holds global settings for the application

var App AppSettings

App is a global registry for application variables

type Client

type Client struct {
	Rooms []string
	// contains filtered or unexported fields
}

Client represents a single connected client

func (*Client) Broadcast

func (c *Client) Broadcast(room string, endpoint string, message *Message)

Broadcast sends a message to connected clients joined to the same room with the exception of the "client"

func (*Client) Disconnect

func (c *Client) Disconnect()

Disconnect forces a client to be disconnected from the server

func (*Client) Join

func (c *Client) Join(room string)

Join joins a user to a broadcast room

func (*Client) Leave

func (c *Client) Leave(room string)

Leave removes a user from a broadcast room

func (*Client) LeaveAll

func (c *Client) LeaveAll()

LeaveAll removes a user from all broadcast rooms they are currently joined to

type GoMessage

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

type Message

type Message struct {
	ID   int    `json:"id,omitempty"`
	GUID string `json:"guid,omitempty"`
	UUID string `json:"uuid,omitempty"`

	Success bool   `json:"success"`
	Text    string `json:"text,omitempty"`

	Meta map[string]interface{} `json:"meta,omitempty"`
	Body map[string]interface{} `json:"body,omitempty"`
}

Message - Standard message type for Socket communications

func NewFailureMessage

func NewFailureMessage(args ...interface{}) *Message

NewFailureMessage generates a failure message

func NewSuccessMessage

func NewSuccessMessage(args ...interface{}) *Message

NewSuccessMessage generates a success message

func ReadGoMessage

func ReadGoMessage(chMsg chan *GoMessage) (*Message, error)

ReadGoMessage reads from a microservice Go channel of type *GoMessage

func (*Message) WithoutMeta

func (m *Message) WithoutMeta() *Message

WithoutMeta removes meta information before returning a copy of the message

type Microservice

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

Microservice defines a microservice connection

func GetMicroservice

func GetMicroservice(name string) *Microservice

GetMicroservice retrieves a microservice reference from App.Microservices

func (*Microservice) Call

func (m *Microservice) Call(endpoint string, message *Message) (*Message, error)

Call sends a request to the microservice

func (*Microservice) Connect

func (m *Microservice) Connect() (*Microservice, error)

Connect manually connects or reconnects to the microservice

func (*Microservice) Connected

func (m *Microservice) Connected() bool

Connected tells whether or not this microservice's connection is still active and alive

func (*Microservice) Disconnect

func (m *Microservice) Disconnect()

Disconnect manually terminates the connection to the microservice

func (*Microservice) Go

func (m *Microservice) Go(endpoint string, message *Message) chan *GoMessage

Go sends a request to the microservice returning channels

func (*Microservice) Listen

func (m *Microservice) Listen(endpoint string, callback func(message *Message))

Listen registers and event handler for a microservice endpoint

func (*Microservice) Lob

func (m *Microservice) Lob(endpoint string, message *Message) error

Lob sends a request to the microservice that does not require a response

type Plugin

type Plugin interface {
	Activate(app *AppSettings)
	Deactivate(app *AppSettings)
}

Plugin is the framework interface defining a plugin

type Request

type Request struct {
	Endpoint string
	Message  *Message
}

Request represents a single request over an active connection

type Session

type Session struct {
	ID string
}

Session is a data store for a single client connection

Jump to

Keyboard shortcuts

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