Show:

Streams.Message Class

Constructs a message from fields, which are typically returned from the server.

Constructor

Streams.Message

(
  • fields
)

Parameters:

Item Index

Methods

construct

(
  • fields
  • [updateCache=false]
)
Q.Stream static

Constructs a Streams.Message from fields. If the Streams.Message.define() function was not called, uses a default constructor for the message, which simply copies the fields.

Parameters:

  • fields Object

    Provide any message fields here. Requires at least the "type" of the stream.

  • [updateCache=false] Boolean optional

    Whether to update the Message.get cache after constructing the Message

Returns:

Q.Stream:

define

(
  • type
  • ctor
  • methods
)
static

Call this function to set a constructor for a message type

Parameters:

  • type String

    The type of the message, e.g. "Streams/chat/message"

  • ctor String | Function

    Your message's constructor, or path to a javascript file which will define it

  • methods Object

    An optional hash of methods

get

(
  • publisherId
  • streamName
  • ordinal
  • callback
)
static

Get one or more messages, which may result in batch requests to the server. May call Message.get.onError if an error occurs.

Parameters:

  • publisherId String
  • streamName String
  • ordinal Number | Object

    Can be the ordinal, or an object containing one or more of:

    • [withMessageTotals] Array optional

      Highly encouraged if ordinal is an object. All the possible message types to automatically update messageTotals for.

    • [min] Number optional

      The minimum ordinal in the range. If omitted, uses limit.

    • [max] Number optional

      The maximum ordinal in the range. If omitted, gets the latest messages.

    • [limit] Number optional

      Change the max number of messages to retrieve. If only max and limit are specified, messages are sorted by decreasing ordinal.

  • callback Function

    This receives two parameters. The first is the error. If ordinal was a Number, then the second parameter is the Streams.Message, as well as the "this" object. If ordinal was an Object, then the second parameter is a hash of { ordinal: Streams.Message } pairs

getAllInstructions

() Object

Get all the instructions from a message.

Returns:

getInstruction

(
  • instructionName
)

Get the value of an instruction in the message

Parameters:

latestOrdinal

(
  • publisherId
  • streamName
  • [checkMessageCache=false]
)
Integer static

Gets the latest ordinal as long as there is a cache for that stream or that stream's messages. Otherwise it returns 0.

Parameters:

  • publisherId String
  • streamName String
  • [checkMessageCache=false] Boolean optional

    whether to check the Streams.Message cache in addition to the Streams.Stream cache

Returns:

Integer:

post

(
  • msg
  • callback
)
static

Post a message to a stream, so it can be broadcast to all participants, sent to all subscribers, etc. May call Message.post.onError if an error occurs.

Parameters:

  • msg Object

    A Streams.Message object or a hash of fields to post. Must include publisherId and streamName.

  • callback Function

    Receives (err, message) as parameters

seen

(
  • [messageTotal]
)
Number | False

Mark the message as seen, updating the messageTotals

Parameters:

  • [messageTotal] Number | Boolean optional

    Pass the total messages seen of this type. Or, pass true to set the latest messageTotal if any was cached, otherwise do nothing.

Returns:

Number | False:

shouldRefreshStream

(
  • The
  • should
)

This affects how the Streams plugin handles the posting of this message on the front end. If true for a particular message type, the stream is refreshed (if it was cached before) after all the message handlers have run. You can, of course, implement more complex functionality in message handlers.

Parameters:

  • The Type

    type of the message

  • should Boolean

    Whether the stream should be refreshed. Defaults to false.

wait

(
  • publisherId
  • streamName
  • ordinal
  • callback
  • [options]
)
Boolean | Null | Q.Pipe static

Wait until a particular message is posted. Used by Streams plugin to make sure messages arrive in order. Call this with ordinal = -1 to load the latest messages.

Parameters:

  • publisherId String
  • streamName String
  • ordinal Number

    The ordinal of the message to wait for, or -1 to load latest messages

  • callback Function

    Called whenever all the previous messages have been processed. The first parameter is [arrayOfOrdinals] that were processed, where latest < ordinals <= ordinal.

  • [options] Object optional

    A hash of options which can include:

    • [max=5] Number optional

      The maximum number of messages to wait and hope they will arrive via sockets. Any more and we just request them again.

    • [timeout=1000] Number optional

      The maximum amount of time to wait and hope the messages will arrive via sockets. After this we just request them again.

    • [unlessSocket=true] Number optional

      Whether to avoid doing any requests when a socket is attached and user is a participant in the stream

    • [evenIfNotRetained] Boolean optional

      Set this to true to wait for messages posted to the stream, in the event that it wasn't cached or retained.

Returns:

Boolean | Null | Q.Pipe:

Returns false if the cached stream already got this message. Returns true if we decided to send a request for the messages. Returns new Q.Pipe if we decided to wait for messages to arrive via socket. Returns null if no attempt was made because ordinal=-1 and stream wasn't cached. In this last case, the callback is not called.

Events

get.onError

Occurs when Message.get encounters an error loading a message from the server

post.onError

Occurs when Message.post encounters an error posting a message to the server