# How to use timer in IC dfinity rust?

**URL:** https://forum.dfinity.org/t/how-to-use-timer-in-ic-dfinity-rust/22443
**Category:** Developers
**Tags:** Functional-Programming, Discussing
**Created:** [August 29, 2023, 12:40pm UTC](https://forum.dfinity.org/t/how-to-use-timer-in-ic-dfinity-rust/22443 "2023-08-29T12:40:34Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![AliSci](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/alisci/32/33799_2.png) [@AliSci](https://forum.dfinity.org/u/AliSci)
#### Post date: [August 29, 2023, 12:40pm UTC](https://forum.dfinity.org/t/how-to-use-timer-in-ic-dfinity-rust/22443/1 "2023-08-29T12:40:34Z")

</div>

I am combing from django background where we use celery beats and celery for background tasks. Now, we have beats and timer which are two alien options for me.  
However, after checking [Internet Computer Loading](https://internetcomputer.org/docs/current/developer-docs/backend/rust/timers#create-the-timer-dapp-project) I did not get the point from this. Because, my goal is to create websocket system using websocket gate to help many users communicate with each other.  
Goal: I want to create a background task that runs 24/7. In which when user make some actions like Deposit ( I have and IC wallet where users can deposit and withdraw) I want to make a background task that check the deposit and update my data ( increate users balance) and send that to the gateway.

here is a pseudocode

```js
 async fn background_task(){
        async fn onmessage(message) = {
                 STORE.get_user(message.user_id).set_balance(balance);
                 websocketgate.send({
                      channel : message.user_id,
                      new_balance: balance,
                      message: "Your deposit is successful",
                  });
         } 
       websocketgate.onmessage = onmessage
   }

```

---

<div class="post-metadata">

### Author: ![ilbert](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ilbert/32/8717_2.png) [@ilbert](https://forum.dfinity.org/u/ilbert)
#### Post date: [September 8, 2023, 8:26pm UTC](https://forum.dfinity.org/t/how-to-use-timer-in-ic-dfinity-rust/22443/2 "2023-09-08T20:26:38Z")

</div>

You cannot have long running tasks on your canister, because otherwise the consensus on the new canister’s state would never be reached.

If you’re familiar with Javascript, you can use `ic_cdk_timers` like you use `setTimeout` and `setInterval`.  
In your case, you could set up an interval using [set\_timer\_interval](https://docs.rs/ic-cdk-timers/latest/ic_cdk_timers/fn.set_timer_interval.html) that periodically runs a callback that polls the [Ledger canister](https://internetcomputer.org/docs/current/references/ledger). But be aware that every execution of the timer’s callback costs you as an [inter-canister call](https://internetcomputer.org/docs/current/developer-docs/gas-cost) as stated in the [Timers documentation](https://internetcomputer.org/docs/current/developer-docs/backend/periodic-tasks/#timers), so setting a frequent interval will cost you quite a lot.

* * *

> [@AliSci](#):
>
> ```auto
> websocketgate.onmessage = onmessage
> 
> ```

Regarding this line, I’m not getting exactly why you’re declaring the [IC WebSocket]'s ([GitHub - omnia-network/ic-websocket-cdk-rs: IC WebSocket CDK for canisters written in Rust](https://github.com/omnia-network/ic-websocket-cdk-rs)) `on_message` callback in this hypothetical long running task, and not just in the canister’s `init` function, as explained in the [Step 3 of the IC WebSocket tutorial](https://medium.com/@ilbert/websockets-on-the-ic-getting-started-5f8bcdfaabdc).
