Networking

It is not uncommon for games to have some kind of connection to a backend service, perhaps to post scores, handle match making or store saved games in the cloud. Many games also have peer to peer connections where game clients communicate directly with each other, without involvement of a central server.

HTTP requests

Defold can make normal HTTP requests using the http.request() function. Example:

local function handle_response(self, id, response)
	print(response.status, response.response)
end

http.request("https://www.defold.com", "GET", handle_response)

This will make an HTTP GET request to https://www.defold.com. The function is asynchronous and will not block while making the request. Once the request has been made and a server has sent a response it will invoke/call the provided callback function. The callback function will receive the full server response, including status code and response headers. It is also possible to make HTTP POST requests to pass data to the server and to specify request headers. Refer to the API reference to learn more.

Using HTTP requests allows you to connect to and interact with thousands of different services on the internet, but in most cases there’s more to it than simply making an HTTP request. You usually need to use some kind of authenticate, the request data may need to be formatted in a certain way and the response may need to be parsed before it can be used. This can of course be done manually by you but there are also extensions and libraries to take care of this sort of thing for you. Below you’ll find a list of some extensions that can be used to more easily interact with specific backend services:

Check the Asset Portal for even more extensions!

Socket connections

Defold includes the LuaSocket library for creating TCP and UDP socket connections. Creating a socket connection, sending some data and reading a response is a little bit more involved:

local client = socket.tcp()
client:connect("127.0.0.1", 8123)
client:settimeout(0)
client:send("foobar")
local response = client:receive("*l")

This will create a TCP socket, connect it to IP 127.0.0.1 (localhost) and port 8123. It will set timeout to 0 to make the socket non-blocking and it will send the string “foobar” over the socket. It will also read a line of data (bytes ending with a newline character) from the socket. Note that the above example doesn’t contain any kind of error handling. Refer to the API reference to learn more about the functionality available via LuaSocket. The official LuaSocket documentation also contains many examples of how to work with the library. There is also some examples and helper modules in the DefNet library.

WebSocket connections

Defold doesn’t contain any out of the box solution for creating WebSocket connections. For WebSocket connectivity it is recommended to use the Defold-WebSocket extension.