Skip to content

ausrasul/backbone

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Backbone

A Golang library for server client communication, uses gRPC under the hood.

Documentation

Documentation.

Features:

  • Dynamically add/change RPC methods (SetCommandHandler)
  • User defined authentication and authorization (using OnConnect event)
  • Detect and handle disconnects (OnDisconnect event)
  • Transmitts and receives strings/serialized data only.
  • Uses bidirectional gRPC stream under the hood.

Example usage:

Server:

func main() {
    // Instantiate the server
    s := server.New("localhost:1234")

    // Define event handlers
    eventChan := make(chan string, 5)
    s.SetOnConnect(func(s *server.Server, clientId string) {
        eventChan <- "a client disconnected " + clientId
    })
    s.SetOnDisconnect(func(s *server.Server, clientId string) {
        eventChan <- "a client disconnected " + clientId
    })
    
    // Define command handlers
    s.SetCommandHandler("client id 123", "command_1", func(s *server.Server, clientId string, arg string) {
        eventChan <- "command received: " + clientId + " -- " + arg
        // handle the command..
        // ..
        // Can respond to client:
        s.Send(clientId, "response_command_known_at_client", "cmdArg")
    }
    
    // Start server
    go s.Start()

    for in := range eventChan {
        log.Println("server received: ", in)
    }
}

Client:

func main() {
    // Instantiate client
    c := client.New("client id 123", ":1234")
    // Set event handlers
    c.SetOnConnect(func(c *client.Client) { log.Println("connected!") })
    c.SetOnDisconnect(func(c *client.Client) { log.Println("disconnected!") })
    // Set command handlers
    c.SetCommandHandler("response", func(c *client.Client, arg string) {
        log.Println("received command from server , ", arg)
        // handle command
        // ...
        // Can respond to server
        c.Send("command_1", "arguments")
    })
    // connect to server
    c.Start()
    c.Send("test_command", "client is sending this command")
    <-time.After(time.Second * 5)
}

License

MIT

About

Go grpc wrapper

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages