# 'serverEvent' operates within the time of performing the delay in 'draw()'

**URL:** <https://discourse.processing.org/t/serverevent-operates-within-the-time-of-performing-the-delay-in-draw/22548>\
**Category:** Libraries\
**Tags:** homework\
**Created:** [July 11, 2020, 12:04pm UTC](https://discourse.processing.org/t/serverevent-operates-within-the-time-of-performing-the-delay-in-draw/22548 "2020-07-11T12:04:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [July 11, 2020, 12:04pm UTC](https://discourse.processing.org/t/serverevent-operates-within-the-time-of-performing-the-delay-in-draw/22548/1 "2020-07-11T12:04:28Z")

</div>

> import [processing.net](http://processing.net).\*;
> 
> int port = 10002;  
> Server myServer;  
> long COUNT\_NUM = 0;
> 
> void setup()  
> {  
> size(400, 400);  
> background(0);  
> myServer = new Server(this, port);  
> }
> 
> void draw() {  
> background(255);  
> delay(10000);  
> }
> 
> void serverEvent(Server someServer, Client someClient) {  
> COUNT\_NUM++;  
> Client thisClient = myServer.available();  
> if (thisClient !=null) {  
> String whatClientSaid = thisClient.readString();  
> if (whatClientSaid != null) {
> 
> ```
> println(" COUNT : " + str(COUNT_NUM) +" / " + whatClientSaid);
>   
> } 
> 
> ```
> 
> }  
> }

![A1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/39a1c2fc71388631c3e9e0820386e3120d7a2566.jpeg)

‘serverEvent’ operates within the time of performing the delay in ‘draw()’.

What is the principle?

Is it a thread?

And how do you do this? Can it be implemented through threads?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 11, 2020, 6:06pm UTC](https://discourse.processing.org/t/serverevent-operates-within-the-time-of-performing-the-delay-in-draw/22548/2 "2020-07-11T18:06:41Z")

</div>

> [@GWAK](#):
>
> Is it a thread?

Yes, **serverEvent()** is called back by Server’s thread. 🧵

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [July 11, 2020, 10:08pm UTC](https://discourse.processing.org/t/serverevent-operates-within-the-time-of-performing-the-delay-in-draw/22548/3 "2020-07-11T22:08:55Z")

</div>

Dear @GoToLoop

Can’t we make it like’serverEvent’?

There is a delay in’draw()’ when loading photo data, etc., or doing something. At this time,

When sending data processing to the thread, it seems that the’delay’ of’draw()’ will be resolved.

Is there any good way or example?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 11, 2020, 10:31pm UTC](https://discourse.processing.org/t/serverevent-operates-within-the-time-of-performing-the-delay-in-draw/22548/4 "2020-07-11T22:31:54Z")

</div>

- Don’t use [**delay()**](https://processing.org/reference/delay_.html) to control [**draw()**](https://processing.org/reference/draw_.html)’s FPS.
- [**draw()**](https://processing.org/reference/draw_.html) is called back by the sketch’s “Animation Thread”.
- Instead use [**frameRate()**](https://processing.org/reference/frameRate_.html), [**noLoop()**](https://processing.org/reference/noLoop_.html), [**loop()**](https://processing.org/reference/loop_.html) and [**redraw()**](https://processing.org/reference/redraw_.html):

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 11, 2020, 11:12pm UTC](https://discourse.processing.org/t/serverevent-operates-within-the-time-of-performing-the-delay-in-draw/22548/5 "2020-07-11T23:12:19Z")

</div>

Hello,

I am only sharing partial code…

Visual Example on YouTube:  
[Arduino to Processing Server to Processing Client](https://www.youtube.com/watch?v=a1tNKAjXNC8https://www.youtube.com/watch?v=a1tNKAjXNC8)

1. Arduino is sending data to _Processing Server_.
2. _Processing Server_ is _only_ receiving data from _Arduino_ and sending to _Processing Client._  
draw() is not looping; see noLoop() in setup().

> **Processing Client Code**
>
> ```auto
> import processing.serial.*;
> 
> Serial myPort;
> String sRx;
> 
> import processing.net.*;
> 
> Server s;
> String input;
> 
> void setup() 
> {
> size(200, 200);
> background(0);
>   
> s = new Server(this, 12345);
>   
> printArray(Serial.list());
> String portName = Serial.list()[0];
> myPort = new Serial(this, portName, 19200);
> myPort.bufferUntil(' ');
> frameRate(60);
> noLoop();
> }
> 
> void draw() 
> {
> // println(sRx);
> }
> 
> void serialEvent(Serial myPort) 
> {
> sRx = myPort.readString();
> s.write(sRx);
> // println(sRx);
> }
> 
> ```

1. _Processing Client_ is receiving data and buffer it into an array and updates canvas every draw() cycle (60fps default).

References:

> **[Network](https://processing.org/tutorials/network/)**
>
> An introduction to sending and receiving data with clients and servers.

> **[Network / Libraries](https://processing.org/reference/libraries/net/index.html)**
>
> Send and receive data over the Internet through simple clients and servers.

Examples that come with Processing:

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b5878537516032cd60b8a01a48828deb9b1da698.png)

`:)`
