Fab.io is a lightweight real-time game backend framework written in Go (Golang).
- MVC Pattern
- Synchronized Loop Based Actor Model
- Powered by socket.io
To install Fab.io package, you will need to:
- Have Go installed. Head over to Go's download page here to install it.
- Setup your Go workspace.
- Install Fab.io
$ go get -u github.com/kooinam/fab.io
- Import it in your code:
import (
fab "github.com/kooinam/fab.io"
)
In our first example, we start by creating an simple JavaScript chatroom application which will be connecting to backend services using Fab.io.
- Create an empty directory. In this example we shall use
fabio-chat-demo
:
$ mkdir fabio-chat-demo
$ cd fabio-chat-demo
- Create an empty directory
demo
insidefabio-chat-demo
to hold our Javascript application codes.
$ mkdir demo
- Create an HTML file
chat.html
in thedemo
folder and copy the snippet content over intochat.html
.
Now, let's proceed to setup our backend services.
- Use the
go mod
command to manage our package dependencies. Let's go ahead and initialize our package dependencies:
$ go mod init fabio-chat-demo
- Install Fab.io.
$ go get -u github.com/kooinam/fab.io
- Create an empty directory
controllers
insidefabio-chat-demo
to hold our controllers. A controller is responsible for handling any request and producing the appropriate output. Every controller should implement two functionsRegisterBeforeHooks
andRegisterActions
.
$ mkdir controllers
- Create an go file
chat_controller.go
incontrollers
folder. Put the following snippet content intochat_controller.go
.
package controllers
import (
fab "github.com/kooinam/fab.io"
"github.com/kooinam/fab.io/controllers"
"github.com/kooinam/fab.io/helpers"
)
// ChatController used for chat purposes
type ChatController struct {
}
// RegisterHooksAndActions used to register hooks and actions
func (controller *ChatController) RegisterHooksAndActions(hooksHandler *controllers.HooksHandler, actionsHandler *controllers.ActionsHandler) {
actionsHandler.RegisterAction("Join", controller.join)
actionsHandler.RegisterAction("Message", controller.message)
}
// join used for player to join a room
func (controller *ChatController) join(context *controllers.Context) {
roomID := context.ParamsStr("roomID")
// leave all previously joined rooms, and join new room
context.SingleJoin(roomID)
}
// message used for player to send message message to room
func (controller *ChatController) message(context *controllers.Context) {
roomID := context.ParamsStr("roomID")
message := context.ParamsStr("message")
// broadcast message to room
fab.ControllerManager().BroadcastEvent("chat", roomID, "Message", nil, helpers.H{
"message": message,
})
}
- Lastly, create
main.go
in root directory and put the following snippet content intomain.go
.
package main
import (
"fabio-chat-demo/controllers"
"net/http"
fab "github.com/kooinam/fab.io"
)
func main() {
fab.Setup()
fab.ControllerManager().RegisterController("chat", &controllers.ChatController{})
fab.ControllerManager().Serve("8000", func() {
fs := http.FileServer(http.Dir("./demo"))
http.Handle("/demo/", http.StripPrefix("/demo/", fs))
})
}
Congrats! Now all that's left to do is run the app!
- Start our application by running:
go run main.go
- Navigate to
http://localhost:8000/demo/chat.html
on your browser to see your chatroom application in action!
Expore more example use cases by reading our Wiki!
- Wiki
- https://github.com/kooinam/fabio-chat-demo - An simple chatroom demo with demonstrations of using routings and controllers.
- https://github.com/kooinam/fabio-demo - An simple tic-tac-toe demo with demonstrations of an MVC pattern architecture.
Package | Link |
---|---|
go-socket.io | github.com/googollee/go-socket.io |
Some of our upcoming key feature(s)/improvement(s) include:
- Write MORE Tests
- Tutorials and Documentations
- Containerize Solutions
- Distributed Solutions
- Graceful Shutdown
- Actor Model
Distributed under the MIT License.