# publish()

### publish(topicURI\[, payload\[, advancedOptions]])

Publish a new event to topic.

Parameters:

* **topicURI**. Required. A string that identifies the topic. Must meet a WAMP Spec URI requirements.
* **payload**. Publishing event data. Optional. Maybe any single value or array or hash-table object or null:
  * If it is an array - it is sent as is as WAMP positional arguments attribute
  * If it is a single object (without **argsList** and **argsDict** keys) - it is sent AS IS as WAMP key-value arguments attribute
  * If it is a single Number/String/Boolean/Null value - it is packed into a one-item array and is sent as WAMP positional arguments attribute. Be aware: the receiver will get it in **argsList** as a one-item array and not as a single value!
  * It is possible to pass the array and object-like data simultaneously. In this case, pass a hash-table with the next attributes:
    * **argsList**: array payload (may be omitted)
    * **argsDict**: object payload (may be omitted)
* **advancedOptions**. Optional parameters hash table. Must include any or all of the options:
  * **exclude**: integer|array WAMP session id(s) that won't receive a published event, even though they may be subscribed
  * **exclude\_authid**: string|array Authentication id(s) that won't receive a published event, even though they may be subscribed
  * **exclude\_authrole**: string|array Authentication role(s) that won't receive a published event, even though they may be subscribed
  * **eligible**: integer|array WAMP session id(s) that are allowed to receive a published event
  * **eligible\_authid**: string|array Authentication id(s) that are allowed to receive a published event
  * **eligible\_authrole**: string|array Authentication role(s) that are allowed to receive a published event
  * **exclude\_me**: bool flag of receiving publishing event by initiator (if it is subscribed to this topic)
  * **disclose\_me**: bool flag of disclosure of publisher identity (its WAMP session ID) to receivers of a published event
  * **ppt\_scheme**: string Identifies the Payload Schema for Payload Passthru Mode
  * **ppt\_serializer**: string Specifies what serializer was used to encode the payload
  * **ppt\_cipher**: string Specifies the cryptographic algorithm that was used to encrypt the payload
  * **ppt\_keyid**: string Contains the encryption key id that was used to encrypt the payload

Returns `promise`:

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

```javascript
await ws.publish('user.logged.in');
await ws.publish('chat.message.received', 'user message'); // will be sent as ['user message1']
await ws.publish('chat.message.received', ['user message1', 'user message2']);
await ws.publish('user.modified', { field1: 'field1', field2: true, field3: 123 });
await ws.publish('chat.message.received', ['Private message'], { eligible: 123456789 });

try {
    await ws.publish('user.modified', { field1: 'field1', field2: true, field3: 123 });
    console.log('User successfully modified');
} catch (e) {
    console.log('User modification failed', e.error, e.details);
}
```


---

# 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/publish.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.
