# N2O Protocol

# Requests

## pickle

Picled messaged are used if you send messages over unencrypted channel and want to hide the content of the message, that was generated on server. You can use BASE64 pickling mechanisms with optional AES/RIPEMD160 encrypting. `ws.send(enc( tuple(atom('pickle'), binary('ddtake'), binary('g2gCaAVkAAJldmQABWluZGV4ZAAEdGFrZWsABH'+ Rha2VkAAVldmVudGgDYgAABXViAAQKXmIAC3cK'), [tuple(atom('ddtake'),'0')])));` Where Base64 represents the N2O EVENT: `#ev{module=index,payload=take,trigger="take",name=event}`

This is Nitrogen-based messaging model.
This request will return JSON with EVAL field only.

## client

Client messages usually originated at client and represent the Client API Requests:

`ws.send(enc( tuple( atom('client'), tuple(atom('join_game'),1000001))));`

NOTE: This request may return JSON with EVAL and DATA fields.

## bert

When you want to receive BERT messages on client you usually ask the server by sending:

`ws.send(enc( tuple( atom('bert'), binary('API Request'));`

This messages could be handled as this:

`event({bert,Message}) -> wf:info("This API will return BERT enveloped echo"), {answer,echo,Message};`

NOTE: you will always receive BERT messages.

## binary

When you need raw binary Blob on client side, for images or other raw data you can ask server like this:

`ws.send(enc( tuple( atom('binary'), binary('API Request'));`

And handle also in binary clause:

`event({binary,Message}) -> wf:info("This API will return Raw Binary"), <<84,0,0,0,108>>;`

NOTE: if event returns not the binary client will recieve BERT encoded message.

## server

Server messages are usually being sent to client originated on the server by sending info notifications directly to Web Socket process: `> WebSocketPid ! {server, Message}`

You can obtain this Pid like here:

`event(init) -> wf:info("Web Socket Pid: ~p",[self()]);`

You can also send server messages from client relays:

`ws.send(enc( tuple( atom('server'), binary('Binary Message'));`

NOTE: This request may return JSON with EVAL and DATA fields.

# Responses

## JSON envelop

Each message from Web Socket channel to Client encoded as JSON object. N2O.js is used to decode WebSocket binary messages from JSON container.

`{ "eval": "ws.send("Send Back This String");", "data": [131,104,2,100,0,7,109,101,115,115, 97,103,101,107,0,5,72,101,108,108,111] }`

EVAL values evaluated immediately and DATA values passed to handle_web_socket(data) function if exists.

`function handle_web_socket(body) { console.log(body); }`

## JSON enveloped BERT

Usually in DATA come BERT messages (Binary Erlang Term Format). js is used to decode Game Protocol message.

`function handle_web_socket(body) { console.log(String(dec(body))); }` `E> Received: {message,"Hello"}`

## BERT binary

`function handle_web_socket(body) { console.log(String(dec(body))); }`

## RAW binary

`function handle_web_socket_blob(body) { }` `E> Unknown Raw Binary Received: [72,101,108,108,111]`