Mysql database connection

Hi,

I know that in lua it is possible to implement a tcp/ip based Mysql client by loading a socket module and then creating the tcp/ip connection.

I need to get the values ​​from a table in Mysql database to display it on screen.
Is this approach possible on info-beamer hosted , or will there be any restrictions?

Thanks!

The info-beamer process embedding the Lua scripting abilities isn’t intended to make external connections. It’s not possible to load external modules or make connections to other servers from within Lua. The intended way to implement that is by adding a package service alongside your visual Lua code.

The way this works is that you add a script or binary file named service to your package. When such a package is then assigned to the device, the service file is marked executable and ensured to run in the background. You can then do all kinds of background processing there and for example generate a JSON file with the data needed for the display logic.

In your Lua code you can use:

local title = "<loading..>"
node.json_watch("data.json", function(data)
     title = data.title
end)

This will instruct info-beamer to watch for any changes to the file data.json. The callback will be called and you can immediately update your internal state based on the values within that JSON file. In the example the value in the title variable changes if you provide a new JSON content like {"title": "Breaking news: Something!"}.

As for the package service: If possible, I would recommend using golang to implement them as it’s almost trivial to cross-compile such a service from your developer machine and the resulting single static binary, which you can then name service makes it very easy to deploy.

Minimal example again:

// this is the file main.go
package main

import (
        "encoding/json"
        "fmt"
        "io/ioutil"
)

func main() {
        data := map[string]string{
                "title": "Something",
        }

        jsonData, err := json.MarshalIndent(data, "", "  ")
        if err != nil {
                fmt.Println("Error marshaling JSON:", err)
                return
        }

        err = ioutil.WriteFile("data.json", jsonData, 0644)
        if err != nil {
                fmt.Println("Error writing JSON file:", err)
                return
        }
}

Cross compile to arm using:

GOOS=linux GOARCH=arm go build -ldflags="-s -w" -o service main.go

The resulting service file can be directly added to the package and will create a data.json file read by the Lua code above. Using mysql from Golang shouldn’t be too difficult and the deployment flow is identical: Build that static binary and include it in the info-beamer package. Hope that helps?

Hi,
Thanks for the detailed answer, it helps me a lot. It became clear to me how to get data from mysql by creating the binary file, which from what you tell me is quite simple with golang. Thanks for the guide on how to do it.