# How to TimeOut when Calling RPC in Unity?

**URL:** <https://forum.heroiclabs.com/t/how-to-timeout-when-calling-rpc-in-unity/4192>\
**Category:** Client Libraries\
**Tags:** sdk-unity\
**Created:** [August 4, 2023, 7:18pm UTC](https://forum.heroiclabs.com/t/how-to-timeout-when-calling-rpc-in-unity/4192 "2023-08-04T19:18:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spuddy](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@Spuddy](https://forum.heroiclabs.com/u/Spuddy)\
**Post date:** [August 4, 2023, 7:18pm UTC](https://forum.heroiclabs.com/t/how-to-timeout-when-calling-rpc-in-unity/4192/1 "2023-08-04T19:18:25Z")

</div>

I’ve written a RPC function that I call from the Unity SDK that simply pings the server to see if it’s online and the player is authenticated. Can somebody show me how to modify the following code to apply a 5 second time out ? Will the catch statement be called when it times out ?

```auto
try {
  await m_Client.RpcAsync(mysession, "ping", "");  
}
catch (Nakama.ApiResponseException ex) {
  // failed
}

```

---

<div class="post-metadata">

**Author:** ![novabyte](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/novabyte/32/1324_2.png) [@novabyte](https://forum.heroiclabs.com/u/novabyte)\
**Post date:** [August 6, 2023, 1:41pm UTC](https://forum.heroiclabs.com/t/how-to-timeout-when-calling-rpc-in-unity/4192/2 "2023-08-06T13:41:09Z")

</div>

@Spuddy There’s another function parameter you can pass into all the functions in the SDK which is named `canceller` and expects a `CancellationToken`. This is how you can provide a maximum time for the function to execute for.

```plaintext
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
m_Client.RpcAsync(mysession, "ping", "", canceller: cts.Token);
// Will throw a TaskCancelledException if a timeout occurs.

```

All of this is standard .NET functionality and not Unity engine specific:

- [CancellationTokenSource](https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtokensource?view=netframework-4.5)
- [CancellationToken](https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken?view=netframework-4.5)

Hope this helps.

---

<div class="post-metadata">

**Author:** ![mofirouz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/mofirouz/32/1523_2.png) [@mofirouz](https://forum.heroiclabs.com/u/mofirouz)\
**Post date:** [August 7, 2023, 2:16pm UTC](https://forum.heroiclabs.com/t/how-to-timeout-when-calling-rpc-in-unity/4192/3 "2023-08-07T14:16:48Z")

</div>

@gabriel probably good to add to Unity Guide in our docs plz.

---

<div class="post-metadata">

**Author:** ![gabriel](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/gabriel/32/644_2.png) [@gabriel](https://forum.heroiclabs.com/u/gabriel)\
**Post date:** [August 7, 2023, 3:00pm UTC](https://forum.heroiclabs.com/t/how-to-timeout-when-calling-rpc-in-unity/4192/4 "2023-08-07T15:00:39Z")

</div>

For reference in case anyone lands on this topic, it is included in this section of the guide: [Heroic Labs Documentation | Unity/.Net](https://heroiclabs.com/docs/nakama/client-libraries/unity/index.html#handling-retries)

But will try and make it more visible 👍

---

<div class="post-metadata">

**Author:** ![Spuddy](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@Spuddy](https://forum.heroiclabs.com/u/Spuddy)\
**Post date:** [August 10, 2023, 1:25am UTC](https://forum.heroiclabs.com/t/how-to-timeout-when-calling-rpc-in-unity/4192/5 "2023-08-10T01:25:56Z")

</div>

I’ve tried what you said and created a CancellationToken with a time out of 2 seconds and also set a timeout of 10 seconds on the Nakama client but it’s taking around 75 seconds to throw the exception so I don’t understand what’s going on ? I’ve tested in the Unity Editor on a Mac but haven’t tried on a device yet

Update: It seems to time out after the expected 2 seconds on my iPad, just takes over a minute in the Unity Editor
