---
title: BSDatagramSocket
slug: develop/bsdatagramsocket
docTags: 
createdAt: 2025-03-11T07:07:17.199Z
---

For more information about available methods, refer to the *roDatagramReceiver* and *roDatagramEvent* entries.

## Methods

- `BindLocalPort(in long port)` boolean&#x20;
- `GetLocalPort()` long&#x20;
- `JoinMulticastGroup(in DOMString group)` boolean&#x20;
- `SendTo(in DOMString dest, in long port, in ArrayBufferView data)` boolean&#x20;
- `SendTo(in DOMString dest, in long port, in DOMString data)` boolean&#x20;
- `Close()` void : Shuts down the instance, preventing it from further consuming resources. If this method is not called, garbage collection determines when the instance will be destroyed.

## Events

The following event is available on the *BSDatagramSocket* object. It can receive an event of the type DatagramSocketEvent. Use getBytes() to retrieve the body of the UDP message.

- `ondatagram`

### DatagramSocketEvent

The DatagramSocketEvent has the following attributes:

- `remoteHost` DOMString readonly &#x20;
- `remotePort` int readonly &#x20;

The DatagramSocketEvent supports the following methods:

- `getBytes()` ArrayBuffer&#x20;

## Example

:::hint{type="info"}
See [here](https://stackoverflow.com/questions/6965107/converting-between-strings-and-arraybuffers) to learn how to convert between strings and an `ArrayBuffer`.
:::

```text
var bsSocketMessage = new BSDatagramSocket();
bsSocketMessage.BindLocalPort(1234)
bsSocketMessage.ondatagram = function(e){

  var txtDec = new TextDecoder("utf-8");
  console.log("Received " + txtDec.decode(e.getBytes()));
};
```

