# DefNet Capability

**URL:** <https://forum.defold.com/t/defnet-capability/71368>\
**Category:** Questions\
**Created:** [July 29, 2022, 11:09am UTC](https://forum.defold.com/t/defnet-capability/71368 "2022-07-29T11:09:13Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Huy\_Nguyen](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/huy_nguyen/32/27982_2.png) [@Huy\_Nguyen](https://forum.defold.com/u/Huy_Nguyen)\
**Post date:** [July 29, 2022, 11:09am UTC](https://forum.defold.com/t/defnet-capability/71368/1 "2022-07-29T11:09:13Z")

</div>

Hi, and sorry for so many questions in a single day here 😓

It’s really simple to make a p2p network and make a LAN multiplayer with DefNet, but is it possible to make online multiplayer that everyone in the world can come and join? If it’s possible, so how could I do so? (But I still want it to be p2p network)

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [July 29, 2022, 11:26am UTC](https://forum.defold.com/t/defnet-capability/71368/2 "2022-07-29T11:26:48Z")

</div>

> [@Huy\_Nguyen](#):
>
> If it’s possible, so how could I do so? (But I still want it to be p2p network)

That is more complicated. I would recommend that you have a central server for the matchmaking and then hand over to the game clients for the gameplay with client authoritative play where one client acts as a server.

It might work if the client that acts as a host is able to share a public IP with the other players somehow. Perhaps as a QR code or link in a message or something.

---

<div class="post-metadata">

**Author:** ![Denis\_Makhortov](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/denis_makhortov/32/24045_2.png) [@Denis\_Makhortov](https://forum.defold.com/u/Denis_Makhortov)\
**Post date:** [July 29, 2022, 1:33pm UTC](https://forum.defold.com/t/defnet-capability/71368/3 "2022-07-29T13:33:31Z")

</div>

If you like the idea of global servers without creating them at runtime, then here is an example of a Cold Path game server implementation using the defnet module  
Sources: [GitHub - jalhund/cold-path-server · GitHub](https://github.com/jalhund/cold-path-server)  
How to use: [https://book.denismakhortov.com/server/summary](https://book.denismakhortov.com/server/summary)

---

<div class="post-metadata">

**Author:** ![masterneme](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/masterneme/32/31665_2.png) [@masterneme](https://forum.defold.com/u/masterneme)\
**Post date:** [July 31, 2022, 4:10am UTC](https://forum.defold.com/t/defnet-capability/71368/4 "2022-07-31T04:10:06Z")

</div>

> [@britzl](#):
>
> It might work if the client that acts as a host is able to share a public IP with the other players somehow. Perhaps as a QR code or link in a message or something.

Like Quake?

 ![quake_join_menu](https://forum-defold.b-cdn.net/uploads/default/original/3X/9/d/9d6094b813fc7a4e89354394be41bbc17e1e55c2.png)

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [July 31, 2022, 7:35am UTC](https://forum.defold.com/t/defnet-capability/71368/5 "2022-07-31T07:35:20Z")

</div>

Yes, exactly!

---

<div class="post-metadata">

**Author:** ![Huy\_Nguyen](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/huy_nguyen/32/27982_2.png) [@Huy\_Nguyen](https://forum.defold.com/u/Huy_Nguyen)\
**Post date:** [August 5, 2022, 8:36am UTC](https://forum.defold.com/t/defnet-capability/71368/6 "2022-08-05T08:36:07Z")

</div>

Hi @britzl, there is a question that I want to ask with DefNet:

- How to send a data from the server to a specific client?

I’ve seen a function with the server is: `server.send(server, data, client)`  
I think this is a function that is used for that purpose, but I didn’t know how to use this function. I think I know what to put in the `data` parameter, but what to put to the `server` and `client`?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [August 5, 2022, 10:29am UTC](https://forum.defold.com/t/defnet-capability/71368/7 "2022-08-05T10:29:11Z")

</div>

> [@Huy\_Nguyen](#):
>
> I’ve seen a function with the server is: `server.send(server, data, client)`  
> I think this is a function that is used for that purpose, but I didn’t know how to use this function. I think I know what to put in the `data` parameter, but what to put to the `server` and `client`?

Correct, that is the right function to use. You have two ways to use the tcp\_server and tcp\_client modules:

```lua
local tcp_server = require "defnet.tcp_server"

local my_server = tcp_server.create(...)

-- option 1
my_server.start()

-- option 2
tcp_server.start(my_server)

```

The same goes for all of the other functions, such as send:

```lua
my_server.send("some data", client)
tcp_server.send(my_server, "some data", client)

```

So it is just a matter of preference. I prefer the second way where a reference to the server instance is used as the first argument.

Now, the client argument is the open socket connection for the client you wish to send to. You can get a reference to the client from for instance the on\_connected or on\_data callbacks.

---

<div class="post-metadata">

**Author:** ![Huy\_Nguyen](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/huy_nguyen/32/27982_2.png) [@Huy\_Nguyen](https://forum.defold.com/u/Huy_Nguyen)\
**Post date:** [August 6, 2022, 3:23am UTC](https://forum.defold.com/t/defnet-capability/71368/8 "2022-08-06T03:23:51Z")

</div>

> [@britzl](#):
>
> Now, the client argument is the open socket connection for the client you wish to send to. You can get a reference to the client from for instance the on\_connected or on\_data callbacks.

Hi, it’s me again. So to organize clients, I was trying to put them in a table so that can call back to the client whenever I want, but when I do so, I got this error

```lua
ERROR:SCRIPT: defnet/tcp_server.lua:181: main/network/server.script:13: unsupported value type in table: userdata
stack traceback:
  [C]:-1: ?
  defnet/tcp_server.lua:181: in function update
  main/network/server.script:52: in function <main/network/server.script:49>

```

Do you have a solution for this? I know that I can put them in a variable, but it’s not really flexible to reference and organize the data. I don’t really know about the userdata type in lua, so I cannot really think of a solution for this.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [August 6, 2022, 1:10pm UTC](https://forum.defold.com/t/defnet-capability/71368/9 "2022-08-06T13:10:17Z")

</div>

Hmm, what are you doing with the table? Are you using msg.post()? You can’t post userdata (the socket connection in this case).

---

<div class="post-metadata">

**Author:** ![Huy\_Nguyen](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/huy_nguyen/32/27982_2.png) [@Huy\_Nguyen](https://forum.defold.com/u/Huy_Nguyen)\
**Post date:** [August 6, 2022, 1:55pm UTC](https://forum.defold.com/t/defnet-capability/71368/10 "2022-08-06T13:55:41Z")

</div>

> [@britzl](#):
>
> You can’t post userdata (the socket connection in this case).

Actually I’m not post userdata using `msg.post`, but I want to add the client with other data such as name, ip and port, state, etc. to the table in the server script, so that I can organize the data of the client and talk to them easier.  
Right now I think it’s not possible, so is there a way to talk to the specific client right inside the `on_data` function (after the server receive the message, it send a certain data back to that client)?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [August 7, 2022, 6:34am UTC](https://forum.defold.com/t/defnet-capability/71368/11 "2022-08-07T06:34:58Z")

</div>

> [@Huy\_Nguyen](#):
>
> I want to add the client with other data such as name, ip and port, state, etc. to the table in the server script, so that I can organize the data of the client and talk to them easier.  
> Right now I think it’s not possible, so is there a way to talk to the specific client right inside the `on_data` function (after the server receive the message, it send a certain data back to that client)?

Could you please share the code you are using?

---

<div class="post-metadata">

**Author:** ![Huy\_Nguyen](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/huy_nguyen/32/27982_2.png) [@Huy\_Nguyen](https://forum.defold.com/u/Huy_Nguyen)\
**Post date:** [August 7, 2022, 11:30am UTC](https://forum.defold.com/t/defnet-capability/71368/12 "2022-08-07T11:30:19Z")

</div>

> **[Multiplayer Testing – Google Drive](https://drive.google.com/drive/folders/1oKf6T-ejInkLHIyRPhSlbnnh6I-7pJmF?usp=sharing)**

Hi, for the server.script, I put that in \main\network\server.script, could you have a look please?

```lua
local players = {}

local function on_data(data, ip, port, client)
	print("[DATA RECEIVED] TCP server received data '" .. data .. "' from " .. ip .. ":" .. port)
	local data = cjson.decode(data)
	if data.id == "connect" then
		table.insert(players, {ip = ip, port = port, name = data.name, client = client})
		msg.post("server:/server#server", "update_players", {ip = players})
	end
end

```

Here’s what it looks like in the `on_data` function, and the error is also from here.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [August 7, 2022, 12:42pm UTC](https://forum.defold.com/t/defnet-capability/71368/13 "2022-08-07T12:42:45Z")

</div>

> [@Huy\_Nguyen](#):
>
> Actually I’m not post userdata using `msg.post`

> [@Huy\_Nguyen](#):
>
> ```lua
> msg.post("server:/server#server", "update_players", {ip = players})
> 
> ```

But you are posting userdata above! The client is a socket connection object of type “userdata”. You can’t post it since our msg.post code in the engine doesn’t know how to serialize it.

I think what we need is a way to easily look up a client in the tcp server or associate additional data with the clients in the server.

---

<div class="post-metadata">

**Author:** ![Huy\_Nguyen](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/huy_nguyen/32/27982_2.png) [@Huy\_Nguyen](https://forum.defold.com/u/Huy_Nguyen)\
**Post date:** [August 7, 2022, 1:06pm UTC](https://forum.defold.com/t/defnet-capability/71368/14 "2022-08-07T13:06:24Z")

</div>

> [@britzl](#):
>
> But you are posting userdata above!

OMG I’m so sorry for that! I didn’t know why I forgot that when I send the players data that it’s already contain the the userdata 😓😓😓I, all the time, thought that the problem is that I put the userdata in the table 😓

So sorry for taking your time for this, and thank you so much for your help! Have a nice day!

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [August 7, 2022, 2:07pm UTC](https://forum.defold.com/t/defnet-capability/71368/15 "2022-08-07T14:07:49Z")

</div>

> [@Huy\_Nguyen](#):
>
> So sorry for taking your time for this, and thank you so much for your help! Have a nice day!

Not a problem!

---

<div class="post-metadata">

**Author:** ![Huy\_Nguyen](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/huy_nguyen/32/27982_2.png) [@Huy\_Nguyen](https://forum.defold.com/u/Huy_Nguyen)\
**Post date:** [August 9, 2022, 5:42am UTC](https://forum.defold.com/t/defnet-capability/71368/16 "2022-08-09T05:42:35Z")

</div>

> [@DefNet - Defold networking modules](https://forum.defold.com/t/defnet-defold-networking-modules/2940/9):
>
> @Hio: I’ve created a local network multiplayer example here: [https://github.com/britzl/defnet/tree/master/examples/multiplayer](https://github.com/britzl/defnet/tree/master/examples/multiplayer) The example will listen for an instance of the application acting as a host or announce its presence as a host for other clients to connect to if no host is currently broadcasting on the local network. When the host receives a connection its is forwarded to all the other clients. The clients themselves communicate directly with each other and repeatedly send a heartbeat…

Hi @britzl , it’s me again 😓  
Anw, do you still have the code for this example? I’ve clicked the link but seem like the page has been removed/deleted. But the platformer example is exactly what I try to make. I now (kinda) know how to send message, receive message with client and server, looking for ip address using p2p\_discovery. Still, there are so much more to do with multiplayer game like how you can only move your character without moving another? What kind of data to send between players (I don’t think that sending position every frame and their animation is the right way, but instead to send their input? And about the enemy, how to make them in sync and if other players kill it, it should be killed in sync - which means every players see that it got killed?). Is there any standard way of doing it (or like the method that people usually use)?

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [August 9, 2022, 6:22am UTC](https://forum.defold.com/t/defnet-capability/71368/17 "2022-08-09T06:22:14Z")

</div>

> [@Huy\_Nguyen](#):
>
> Anw, do you still have the code for this example? I’ve clicked the link but seem like the page has been removed/deleted. But the platformer example is exactly what I try to make

You can browse old versions by going through the commit history on GitHub to find when the multiplayer example was added. Here it is:

> **[defnet/examples/multiplayer at f093b8c6c257d1e565fea1571d6335200696d63c ·...](https://github.com/britzl/defnet/tree/f093b8c6c257d1e565fea1571d6335200696d63c/examples/multiplayer)**
>
> Defold networking examples. Contribute to britzl/defnet development by creating an account on GitHub.

It is old and it was removed for a reason (I can’t remember why exactly but I was probably not happy with the results).

> [@Huy\_Nguyen](#):
>
> how you can only move your character without moving another? What kind of data to send between players (I don’t think that sending position every frame and their animation is the right way, but instead to send their input? And about the enemy, how to make them in sync and if other players kill it, it should be killed in sync - which means every players see that it got killed?).

There are many ways in which to achieve this:

- **Client authoritative multiplayer** - Here you trust the players and let each of them handle their own simulation and only send state changes to the other clients (“I shot a bullet at position P”, “I am at position P” or “I killed enemy X”).
- **Server authoritative multiplayer** - In this scenario you do not trust the players at all. You run the game on the server and the clients send any actions they do to the server and get a world state back. It is unlikely that you will be able to send client updates each frame so instead you let the clients simulate and predict what the clients do and only correct their simulations if they deviate enough from the server (“Player X moved in direction D last frame. I predict that Player X will move in direction D this frame too”).

> [@Huy\_Nguyen](#):
>
> Is there any standard way of doing it (or like the method that people usually use)?

You should read up on the different solutions. I’m sure there are many great articles out there!

Here’s one on the networking in the Source Engine (TF, HalfLife etc):

> **[Making sure you're not a bot!](https://developer.valvesoftware.com/.within.website/?redir=https:%2F%2Fdeveloper.valvesoftware.com%2Fwiki%2FSource_Multiplayer_Networking)**
