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

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

Last updated