Skip to content

Instantly share code, notes, and snippets.

@theturtle32
Created September 6, 2011 09:41
Show Gist options
  • Save theturtle32/1197134 to your computer and use it in GitHub Desktop.
Save theturtle32/1197134 to your computer and use it in GitHub Desktop.
WebSocket streaming api

The methods and events here are in addition to the ones already defined in the documentation for WebSocket-Node

WebSocketConnection

Methods

###newMessage(type)

type can be either "text" or "binary". Returns an instance of WebSocketWritableStream, a writable stream that represents a single logical WebSocket message.

This method replaces both sendBytes(buffer) and sendUTF(string) on WebSocketConnection

Events

###message(type, stream)

type will be either "text" or "binary". stream is WebSocketReadableStream, a readable stream that represents a single logical WebSocket message.

This event replaces the existing 'message' and 'frame' events on WebSocketConnection*

WebSocketReadableStream

Methods

###destroy()

Stops emitting data events for the incoming message, effectively ignoring the rest of the message's content. Any further data received in this message will be silently discarded

###pause()

Pauses the incoming data events.

###resume()

Resumes the incoming data events after a pause().

###pipe(destination, [options])

This is a Stream.prototype method available on all Streams.

Connects this read stream to destination WriteStream. Incoming data on this stream gets written to destination. The destination and source streams are kept in sync by pausing and resuming as necessary.

See the Node.JS documentation on streams for more detail.

###setEncoding(encoding)

Makes the data event emit a string instead of a Buffer. encoding can be binary, utf8, ascii, or base64.

Events

###data function(data)

data is a Buffer by default if the message type is binary or a string if the message type is utf8 or after setEncoding() set called with an encoding other than binary. This event is emitted for each TCP packet or WebSocket frame received for this logical WebSocket "message." When working with a utf8 message, the stream will need to be aware of utf-8 encoding and be careful to look for a partial utf-8 character at the end of a frame or tcp packet. Some minimal buffering will need to be done on those bytes, prepending them to the following frame or packet in order to continue correctly emitting characters. WebSocket frames are not guaranteed to split cleanly on unicode character boundaries.

###end function()

Emitted when the WebSocket message is complete and all data has been emitted.

###error function(exception)

Emitted when there is an error while receiving the WebSocket message. It could be a protocol error or an error on the underlying connection, e.g. if the TCP socket is suddenly closed or a close frame is received before the end of a fragmented message.

Properties

###readable

Always true until an end or error event has occurred.

WebSocketWritableStream

Represents a single outgoing logical WebSocket message.

Methods

###write(string) ###write(buffer)

Writes string or buffer as a WebSocket logical fragment of the message. If a String is passed as the argument, it will be encoded as utf-8 per the WebSocket specification. No other text encodings are allowed. Returns true if the data has been flushed to the kernel buffer. Returns false to indicate that the kernel buffer is full, and the data will be sent out in the future. The drain event will indicate when the kernel buffer is empty again.

###end(string) ###end(buffer) ###end()

Writes the last (or only) frame of the WebSocket logical message to the socket and closes the stream. The last frame will have its FIN bit set indicating that the message is complete. If you want to send a single frame message, use this method instead of write().

If no argument is passed to end(), a FIN frame will be written to the socket with no payload, completing the WebSocket message.

After calling end(), the stream will not emit any more events.

###destroy() ###destroySoon()

Aliases for end()

Events

###drain function()

Emitted after a write() method was called that returned false to indicate that it is safe to write again.

###error function(exception)

Emitted on error with the exception exception. The error is most likely to be an error with the underlying connection, such as attempting to write to a closed socket.

###close function()

Emitted when the underlying WebSocket connection has been closed.

###pipe function()

Emitted when the stream is passed to a readable stream's pipe method.

Properties

###writable

A boolean that is true by default, but turns false after an error occurred or end() / destroy() was called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment