##Examples

### Running the examples

Open two terminals. In order to run example 1, in one terminal, start the server:

```bash
cd myproject/node_modules/rpc-websocket
node doc/examples/1-send-server.js
```
In the other terminal, you can execute the client:

```bash
cd myproject/node_modules/rpc-websocket
node doc/examples/1-send-client.js
```

### Example 1

For: _Sending/receiving typed messages_

**Programs**

_The client_

```javascript
!INCLUDE "doc/examples/1-send-client.js"
```

_The server_

```javascript
!INCLUDE "doc/examples/1-send-server.js"
```

**Output**

_The client_

        something back

_The server_

        server started ...
        received: something
        received: something

As you can see, you can just resort to a naming convention to create something like a _test_ channel or namespace.

### Example 2

For: _Making RPC calls_

You can let the client make RPC calls to the server, but you can also let the server make RPC calls to the client. Server-to-client RPC calls are not possible with ajax. The fact that this is not possible, endlessly complicates the construction of particular types of applications such as real-time chat boxes.

**Programs**

_The client_

```javascript
!INCLUDE "doc/examples/2-rpc-client.js"
```

_The server_

```javascript
!INCLUDE "doc/examples/2-rpc-server.js"
```

**Output**

_The client_

        received the following reply:something back

_The server_

        server started ...
        received: something

### Example 3

For: _Handling before/after send/receive events_

You can use the `beforeSend` event to make changes to the message that is about to be sent. You can use the `afterSend` event to do some logging, for example, after successfully sending a message. You can also use the `beforeReceive` and `afterReceive` events. Here an example:

**Programs**

_The client_

```javascript
!INCLUDE "doc/examples/3-before-after-send-client.js"
```
_The server_

```javascript
!INCLUDE "doc/examples/3-before-after-send-server.js"
```

**Output**

_The client_

        before sending:{"data":"changed before sending","messageType":"test-type"}
        after sending:{"data":"changed before sending","messageType":"test-type"}
        before receiving:{"data":"something back","messageType":"test-type"}
        something back
        after receiving:{"data":"something back","messageType":"test-type"}

_The server_

        server started ...
        received: changed before sending


### Example 4

For: _Looping over RPC calls_

You could easily run into very subtle bugs when you start looping over RPC calls. For example:

```javascript
!INCLUDE "test/2-rpc-client.js"
```

While the program is executing the reply logic, the value of the loop's counter `i` is not what you may think it is. Since the `reply` logic gets executed asynchronously, your socket could be waiting for a while before getting a response. In the meanwhile your loop counter will have moved on.

If you loop over an RPC call, you can generally not count of the fact that the variables that went into the request, are still the same as when you started the RPC call. Therefore, you must make sure to permanently fix their values in an enclosure:

```javascript
while(condition) {

        ws.rpc('f',[x1,x2],function(y) {

                (function(x1,x2) {

                        /* do something with y */

                })(x1,x2);

        });
}
```
When the enclosure function exits, the logic inside of the function will hang on to copies of `x1`, and `x2` inside its function closure, with values as they were at the moment that the program finished executed the function. The technique to create such enclosure function is called [IIFE](http://en.wikipedia.org/wiki/Immediately-invoked_function_expression) (Immediately-invoked function expression).

