Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface WebSocket

A WebSocket connection that is valid from open to close event. Read more about this in the user manual.

Hierarchy

  • WebSocket

Indexable

[key: string]: any

Arbitrary user data may be attached to this object. In C++ this is done by using getUserData().

Index

Methods

close

  • Forcefully closes this WebSocket. Immediately calls the close handler. No WebSocket close message is sent.

    Returns WebSocket

cork

  • cork(cb: function): void
  • See HttpResponse.cork. Takes a function in which the socket is corked (packing many sends into one single syscall/SSL block)

    Parameters

    • cb: function
        • (): void
        • Returns void

    Returns void

end

  • Gracefully closes this WebSocket. Immediately calls the close handler. A WebSocket close message is sent with code and shortMessage.

    Parameters

    Returns WebSocket

getBufferedAmount

  • getBufferedAmount(): number
  • Returns the bytes buffered in backpressure. This is similar to the bufferedAmount property in the browser counterpart. Check backpressure example.

    Returns number

getRemoteAddress

  • getRemoteAddress(): ArrayBuffer
  • Returns the remote IP address. Note that the returned IP is binary, not text.

    IPv4 is 4 byte long and can be converted to text by printing every byte as a digit between 0 and 255. IPv6 is 16 byte long and can be converted to text in similar ways, but you typically print digits in HEX.

    We will probably add a text converting function at some point as this is a common issue among users.

    Returns ArrayBuffer

getRemoteAddressAsText

  • getRemoteAddressAsText(): ArrayBuffer
  • Returns the remote IP address as text.

    Returns ArrayBuffer

ping

  • Sends a ping control message. Returns true on success in similar ways as WebSocket.send does (regarding backpressure). This helper function correlates to WebSocket::send(message, uWS::OpCode::PING, ...) in C++.

    Parameters

    Returns boolean

publish

  • Publish a message to a topic in MQTT syntax. You cannot publish using wildcards, only fully specified topics. Just like with MQTT.

    "parent/child" kind of tree is allowed, but not "parent/#" kind of wildcard publishing.

    NOTE: publish is NOT immediately active, the actual publish takes place at the end of the current event loop iteration. This to efficiently pack and batch outgoing publishes, and to coalesce outgoing messages across multiple topics. Publishing to many different topics, or to the same topic many times, will thus be properly coalesced into one single send syscall and one single SSL block, per socket.

    This also means that publishing to a topic, then immediately subscribing to it in the same very event loop iteration will cause that publish to also affect the late subscription. Think of it as - subscription vs. publish happens in terms of event loop iterations. If you REQUIRE a strict ordering you have to use Loop::defer or process.nextTick accordingly.

    Parameters

    Returns WebSocket

send

  • send(message: RecognizedString, isBinary?: boolean, compress?: boolean): boolean
  • Sends a message. Make sure to check getBufferedAmount() before sending. Returns true for success, false for built up backpressure that will drain when time is given. Returning false does not mean nothing was sent, it only means backpressure was built up. This you can check by calling getBufferedAmount() afterwards.

    Make sure you properly understand the contept of backpressure. Check the backpressure example file.

    Parameters

    • message: RecognizedString
    • Optional isBinary: boolean
    • Optional compress: boolean

    Returns boolean

subscribe

  • Subscribe to a topic in MQTT syntax. Subscription is immediately active. MQTT syntax includes things like "root/child/+/grandchild" where "+" is a wildcard and "root/#" where "#" is a terminating wildcard.

    Parameters

    Returns WebSocket

unsubscribe

  • Unsubscribe from a topic. Returns true on success, if the WebSocket was subscribed. Unsubscription is immediately active.

    Parameters

    Returns boolean

unsubscribeAll

  • unsubscribeAll(): void
  • Unsubscribe from all topics. Immediately active. This is called automatically before any close handler is called, so you never need to call this manually in the close handler of a WebSocket.

    Returns void

Generated using TypeDoc