> For the complete documentation index, see [llms.txt](https://ksdaemon.gitbook.io/wampy.js/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ksdaemon.gitbook.io/wampy.js/api/subscribe.md).

# 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);
}
```
