[erlang-questions] how to reply message using erlang timer module

Igor Clark igor.clark@REDACTED
Sat Sep 23 12:54:46 CEST 2017


You could try timer:send_interval/3 
<http://erlang.org/doc/man/timer.html#send_interval-3>:

> send_interval(Time, Pid, Message)
> Evaluates Pid ! Message repeatedly after Time milliseconds. (Pid can 
> also be an atom of a registered name.)

There's even a timer:send_interval/2 
<http://erlang.org/doc/man/timer.html#send_interval-2> that assumes 
self() as the Pid, which looks like it might be what you want:

> send_interval(Time, Message)
> Same as send_interval(Time, self(), Message).

One thing to note though is that the Common Caveats 
<http://erlang.org/doc/efficiency_guide/commoncaveats.html#id60818> 
section says:

> Creating timers usingerlang:send_after/3 
> <http://erlang.org/doc/man/erlang.html#erlang:send_after-3>anderlang:start_timer/3 
> <http://erlang.org/doc/man/erlang.html#erlang:start_timer-3>, is much 
> more efficient than using the timers provided by thetimer 
> <http://erlang.org/doc/man/timer.html>module in STDLIB. Thetimermodule 
> uses a separate process to manage the timers. That process can easily 
> become overloaded if many processes create and cancel timers 
> frequently (especially when using the SMP emulator).
>

I'm not sure if that applies to send_interval/{2,3} as well, or quite 
how many "many" means there - I guess it's probably "a really large 
number", but perhaps somebody could jump in here?

But if so, and if you expect heavy traffic with an interval like this on 
every WS connection, an alternative might be to create a loop to send 
the message using send_after/3 instead of using the timer module. I.e. 
the process which handles Msg could call erlang:send_after( Time, 
self(), Msg ) when it's done its other work. Working out which process 
does that would depend on your app design - it might work to just do it 
in the process which handles the message you're sending, or you might 
need another process.

Cheers
i

On 23/09/2017 11:21, Palanikumar Gopalakrishnan wrote:
> Hi lgor Clark,
>                        Thanks for the help.  yes you  are correct. Its 
> there any way to send message to websocket using 
> *timer:apply_interval/4*. I didn't find any examples.
>
>
>
>
> On 23 September 2017 at 15:47, Igor Clark <igor.clark@REDACTED 
> <mailto:igor.clark@REDACTED>> wrote:
>
>     Hi Palanikumar,
>
>     It looks like you're passing the erlang:start_timer/3
>     <http://erlang.org/doc/man/erlang.html#start_timer-3> arguments to
>     timer:apply_interval/4
>     <http://erlang.org/doc/man/timer.html#apply_interval-4>:
>
>     erlang:start_timer(Time, Dest, Msg)
>
>     vs
>
>     apply_interval(Time, Module, Function, Arguments)
>
>     erlang:start_timer/3 sends Msg to Dest after Time ms;
>     timer:apply_interval/4 calls Module:Function(Arguments) every Time ms.
>
>     So the error message is telling you that the function
>     timer:apply_interval/3 you're trying to call doesn't exist.
>
>     HTH,
>     Igor
>
>
>     On 23/09/2017 10:50, Palanikumar Gopalakrishnan wrote:
>>     Hi Guys,
>>
>>                  I want  to send  message to the front-end via
>>     websocket in particular time interval.
>>     So i use timer module for this purpose.
>>     *timer:apply_interval(1000, self(), <<"How' you doin'?">>),*
>>     *
>>     *
>>     *
>>     *
>>     But it returns the following error
>>
>>     *15:18:20.558 [error] CRASH REPORT Process <0.570.0> with 0
>>     neighbours crashed with reason: call to undefined function
>>     timer:apply_interval(6000, <0.570.0>, <<"How' you doin'?">>)
>>     15:18:20.558 [error] Ranch listener my_cowboy_ws_server
>>     terminated with reason: call to undefined function
>>     timer:apply_interval(6000, <0.570.0>, <<"How' you doin'?">>)
>>
>>
>>     *
>>     Its there any way to solve this problem.
>>
>>     I already tried with
>>
>>     *erlang:start_timer(60000, self(), <<"How' you doin'?">>)
>>
>>     *
>>     Its only send message after particular timer. I didn't found
>>     method in this module to send message in particular interval.
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>     -- 
>>
>>     *Warm Regards,*
>>
>>     *Palanikumar Gopalakrishnan *✌
>>     *Developer*
>>
>>
>>
>>
>>     _______________________________________________
>>     erlang-questions mailing list
>>     erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
>>     http://erlang.org/mailman/listinfo/erlang-questions
>>     <http://erlang.org/mailman/listinfo/erlang-questions>
>
>
>
>
> -- 
>
> *Warm Regards,*
>
> *Palanikumar Gopalakrishnan *✌
> *Developer*
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170923/db4a4e3d/attachment.htm>


More information about the erlang-questions mailing list