# subscribe()

### subscribe(topicURI, onEvent\[, advancedOptions])

Subscribes for topicURI events.

Input Parameters:

* **topicURI**. Required. A string that identifies the topic. Must meet a WAMP Spec URI requirements.
* **onEvent**. Published event callback. Will be called on receiving published event with one hash-table parameter with following attributes:
  * **argsList**: array payload (maybe omitted)
  * **argsDict**: object payload (maybe omitted)
  * **details**: some publication options object.
* **advancedOptions**. Optional parameters hash table. Must include any or all of the options:
  * **match**: string matching policy ("prefix"|"wildcard")

Returns `promise`:

* Resolved with one hash-table parameter with following attributes:
  * **topic**
  * **requestId**
  * **subscriptionId**
  * **subscriptionKey**
* Rejected with one of the [Errors instances](broken://pages/e80SGqRyzZXRlbWTYu1Y)

```javascript
await ws.subscribe('chat.message.received', function (eventData) { console.log('Received new chat message!', eventData); });

try {
    let res = await ws.subscribe('some.another.topic',
        function (eventData) { console.log('Received topic event', eventData); }
    );
    console.log('Successfully subscribed to topic: ' + res.topic);

} catch (e) {
    console.log('Subscription error:' + e.error);
}
```


---

# 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://ksdaemon.gitbook.io/wampy.js/api/subscribe.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.
