# WebSockets

Created for the need of low latency and high bandwidth web app traffic like that of social media chat windows.&#x20;

WebSocket features:

* Full duplex (send and rec simultaneously)
* Uses HTTP proto and port 443/80
* ws\:// (websocket) and wss\:// (secure websocket)
* no polling, send when you have data.

### Basic WS usage:

{% tabs %}
{% tab title="Connect" %}

```javascript
var ws=new WebSocket('ws://<WebsocketServerURL>');
```

{% endtab %}

{% tab title="Sending Message" %}

```javascript
ws.onopen=function(e){
alert("Conenction Made");
this.send('<Your message>');
)
```

{% endtab %}

{% tab title="Receive Message" %}

```
ws.onmessage=function(e){
alert("Recieved Message");
var msg=e.data;
alert(msg);
}
```

{% endtab %}
{% endtabs %}

{% hint style="danger" %}
WebSockets themselves are not insecure but they do fall victim to vulnerabilities in how the data is used if unsanitized. &#x20;
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.hackbook.io/web-application-hacking/web-techniques/html5/websockets.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
