openHAB provides direct access to the [Event Bus](../developer/utils/events.html) through WebSockets.
The WebSocket API allows subscribing to all events (by default) or a sub-set of events which are configurable at runtime via messages.
All messages on the WebSocket connection are JSON encoded text-messages.
## Establishing a connection
WebSockets are available on the same ports as the REST API, usually port 8080 for unsecured (ws-protocol) and port 8443 for secured (wss-protocol) connections.
The connection is established by connecting to `ws[s]://{URL}:{PORT}/ws`.
To prevent unauthorized use of the connection an `accessToken` has to be sent with the initial request.
Two authentication methods are available:
1. API token: `oh.ohwstest.tz1IDPniKLxc0VU4t9tz4GiAiKmc0ZDdMKxhlD5tfviQStM4oNsywrcrUTktPbBE9YQ3wnMBrCqVEIhg7Q`
1. Basic Auth with username/password: `dXNlcjpwYXNzd29yZA==`
## Using the WebSocket connection
### Receiving events (openHAB -> client)
By default, all events on the event bus will be sent as individual messages.
An overview of the most common events can be found [here](../developer/utils/events.html#the-core-events).
The JSON representation of the event always contains the type, topic and payload of the event.
To prevent unnecessary traffic on the connection filters can be applied to the connection.
Filters only work in the direction from openHAB to the client, i.e. even if events from type `ItemCommandEvent` are not subscribed, they can still be sent by the client.
It is not possible to set filters of the same type sequentially.
A new filter message always overrides the settings before.
The default setting is no filter, i.e. all events from all sources.
It is recommended to at least set a source filter for the client itself to prevent event reflection.