Show:

Streams.Stream Class

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

Constructor

Streams.Stream

(
  • fields
)

Parameters:

Methods

actionUrl

(
  • [what='stream']
)
String

A convenience method to get the URL of the streams-related action

Parameters:

  • [what='stream'] String optional

    Can be one of 'stream', 'message', 'relation', etc.

Returns:

String:

The corresponding URL

addPermission

(
  • permission
)

Parameters:

clearAttribute

(
  • attributeName
)

Remove an attribute from the stream, pending to be saved to the server

Parameters:

fileUrl

() String

Calculate the url of a stream's file

Returns:

String:

the url

get

(
  • fieldName
  • usePending
)
Mixed

Get the value of a field

Parameters:

  • fieldName String

    the name of the field to get

  • usePending Boolean

    if true, and there is a value pending to be saved, get that instead

Returns:

Mixed:

getAll

(
  • usePending
)
Object

Get all stream fields

Parameters:

Returns:

getAllAttributes

(
  • usePending
)
Object

Get all stream attributes

Parameters:

Returns:

getAllPermissions

(
  • usePending
)
Array

Parameters:

Returns:

getAttribute

(
  • attributeName
  • usePending
)
Mixed

Get the value of an attribute

Parameters:

  • attributeName String

    the name of the attribute to get

  • usePending Boolean

    if true, and there is a value pending to be saved, get that instead

Returns:

Mixed:

getMessage

(
  • ordinal
  • callback
)

Retrieves a Streams.Message object, by using Message.get

Parameters:

  • ordinal Number

    the ordinal of the message

  • callback Function

    arguments = (err) and this = the message

getParticipant

(
  • userId
  • callback
)

Retrieves a Streams.Participant object, by using Participant.get

Parameters:

  • userId String
  • callback Function

    arguments = (err) and this = the message

hasPermission

(
  • permission
  • usePending
)
Boolean

Parameters:

Returns:

iconUrl

(
  • [size=40]
)
String

Calculate the url of a stream's icon

Parameters:

  • [size=40] Number optional

    the size of the icon to render. Defaults to 40.

Returns:

String:

the url

invite

(
  • [options]
  • callback
)
Q.Request

Invite other users to this stream. Must be logged in first.

Parameters:

  • [options] Object optional

    More options that are passed to the API, which can include:

    • [identifier] String optional

      An email address or mobile number to invite. Might not belong to an existing user yet.

    • [token=false] Boolean optional

      Pass true here to generate an invite which you can then send to anyone however you like. When they show up with the token and presents it via "Q.Streams.token" querystring parameter, the Streams plugin will accept this invite either right away, or as soon as they log in. They will then be added to the list of Streams_Invited for this stream, thus keeping track of who accepted whose invite.

    • [appUrl] String optional

      Can be used to override the URL to which the invited user will be redirected and receive "Q.Streams.token" in the querystring.

    • [userId] String optional

      user id or an array of user ids to invite

    • [platform] String optional

      platform for which uids are passed

    • [uid] String optional

      uid or arary of uids to invite

    • [label] String optional

      label or an array of labels to invite, or tab-delimited string

    • [addLabel] String | Array optional

      label or an array of labels for adding publisher's contacts

    • [addMyLabel] String | Array optional

      label or an array of labels for adding logged-in user's contacts

    • [readLevel] String optional

      the read level to grant those who are invited

    • [writeLevel] String optional

      the write level to grant those who are invited

    • [adminLevel] String optional

      the admin level to grant those who are invited

    • [displayName] String optional

      Optionally override the name to display in the invitation for the inviting user

    • [callback] String optional

      Also can be used to provide callbacks.

    • [followup="future"] Boolean optional

      Whether to set up a followup email or sms for the user to send. Set to true to always send followup, or false to never send it. Set to "future" to send followups only when the invited user hasn't registered yet.

    • [uri] String optional

      If you need to hit a custom "Module/action" endpoint

  • callback Function

    Called with (err, result)

Returns:

Q.Request:

represents the request that was made if an identifier was provided

join

(
  • callback
)

Join a stream as a participant, so you get realtime messages through socket events.

Parameters:

  • callback Function

    receives (err, participant) as parameters

join

(
  • publisherId
  • streamName
  • [callback]
)
static

Join a stream as a participant, so messages start arriving in real time via sockets. May call Streams.join.onError if an error occurs.

Parameters:

  • publisherId String

    id of publisher which is publishing the stream

  • streamName String

    name of stream to join

  • [callback] Function optional

    receives (err, participant) as parameters

leave

(
  • publisherId
  • streamName
  • callback
)
static

Leave a stream that you previously joined, so that you don't get realtime socket messages for that stream anymore. May call Stream.leave.onError if an error occurs.

Parameters:

leave

(
  • callback
)

Leave a stream that you previously joined, so that you don't get realtime messages anymore.

Parameters:

  • callback Function

    Receives (err, participant) as parameters

neglect

(
  • publisherId
  • streamName
  • [callback]
)
static

Stop observing a stream which you previously started observing, so that you don't get realtime messages anymore.

Parameters:

  • publisherId String

    id of publisher which is publishing the stream

  • streamName String

    name of stream to stop observing

  • [callback] Function optional

    receives (err, result) as parameters

neglect

(
  • callback
)

Stop observing a stream which you previously started observing, so that you don't get realtime messages anymore.

Parameters:

  • callback Function

    Receives (err, participant) as parameters

observe

(
  • callback
)

Start observing a stream as an anonymous observer, so you get realtime messages through socket events but you don't join as a participant.

Parameters:

  • callback Function

    receives (err, participant) as parameters

observe

(
  • publisherId
  • streamName
  • [callback]
)
static

Start observing a stream as an anonymous observer, so you get realtime messages through socket events but you don't join as a participant.

Parameters:

  • publisherId String

    id of publisher which is publishing the stream

  • streamName String

    name of stream to observe

  • [callback] Function optional

    receives (err, result) as parameters

post

(
  • [data]
  • callback
)

Post a message to this stream.

Parameters:

  • [data] Object optional

    A Streams.Message object or a hash of fields to post. This stream's publisherId and streamName are added to it.

    • [publisherId] String optional
    • [streamName] String optional
  • callback Function

    Receives (err, message) as parameters

refresh

(
  • callback
  • [options]
)
Boolean

Waits for the latest messages to be posted to a given stream. If your app is using socket.io, then calling this manually is largely unnecessary.

Parameters:

  • callback Function

    This is called when the stream has been refreshed, or if Streams has determined it won't send a refresh request, it will get null as the first parameter.

  • [options] Object optional

    A hash of options, including:

    • [messages] Boolean optional

      If set to true, then besides just reloading the fields, attempt to catch up on the latest messages

    • [max] 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] 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] Number optional

      Whether to avoid doing any requests when a socket is attached

    • [changed] Array optional

      An array of {fieldName: true} pairs naming fields to trigger change events for, even if their values stayed the same

    • [evenIfNotRetained] Boolean optional

      If the stream wasn't retained (for example because it was missing last time), then refresh anyway

    • [extra] Object optional

      Any extra parameters to pass to the callback

Returns:

Boolean:

whether the refresh occurred

refresh

(
  • callback
  • [options]
)
Boolean static

Refreshes a stream, to show the latest content and possibly process the latest messages posted to the stream. If your app server script is running, then calling this manually is largely unnecessary because messages arrive via push using socket.io .

Parameters:

  • callback Function

    This is called when the stream has been refreshed. If the first argument is not false or null, then "this" is the stream. The arguments are different depending on the options. If options.messages is true, then it receives (err, ordinals).

  • [options] Object optional

    A hash of options, including:

    • [messages] Boolean optional

      If set to true, then besides just reloading the fields, attempt to catch up on the latest messages

    • [max] 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] 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] Number optional

      Whether to avoid doing any requests when a socket is attached

    • [changed=null] Object optional

      An Object of {fieldName: true} pairs naming fields to trigger change events for, even if their values stayed the same.

    • [evenIfNotRetained] Boolean optional

      If the stream wasn't retained (for example because it was missing last time), then refresh anyway

    • [extra] Object optional

      Any extra parameters to pass to the callback

Returns:

Boolean:

Whether callback will be called, or false if the refresh has been canceled

relate

(
  • type
  • fromPublisherId
  • fromStreamName
  • [callback]
)

Relates another stream to this stream

Parameters:

  • type String

    the type of the relation

  • fromPublisherId String

    id of publisher of the stream

  • fromStreamName String

    name of stream which is being related to this stream

  • [callback] Function optional

    callback to call with the results First parameter is the error, the second one is an object of Streams.RelatedTo objects you can iterate over with Q.each

relatedFrom

(
  • relationType
  • [options]
  • callback
)

Returns all the streams this stream is related to

Parameters:

  • relationType String

    the type of the relation

  • [options] Object optional

    optional object that can include:

    • [limit] Number optional

      the maximum number of results to return

    • [offset] Number optional

      the page offset that goes with the limit

    • [ascending=false] Boolean optional

      whether to sort by ascending weight.

  • callback Function

    callback to call with the results First parameter is the error, the second one is an object of Streams.RelatedFrom objects you can iterate over with Q.each

relatedTo

(
  • relationType
  • [options]
  • callback
)

Returns all the streams related to this stream

Parameters:

  • relationType String

    the type of the relation

  • [options] Object optional

    optional object that can include:

    • [limit] Number optional

      the maximum number of results to return

    • [offset] Number optional

      the page offset that goes with the limit

    • [ascending=false] Boolean optional

      whether to sort by ascending weight.

    • [prefix] String optional

      optional prefix to filter the streams by

  • callback Function

    callback to call with the results First parameter is the error, the second one is an object of Streams.RelatedTo objects you can iterate over with Q.each

relateTo

(
  • type
  • toPublisherId
  • toStreamName
  • [callback]
)

Relates this stream to another stream

Parameters:

  • type String

    the type of the relation

  • toPublisherId String

    d of publisher of the stream

  • toStreamName String

    name of stream to which this stream is being related

  • [callback] Function optional

    callback to call with the results First parameter is the error, the second one is an object of Streams.RelatedTo objects you can iterate over with Q.each

release

() Q.Streams.Stream

Release the stream in the client retained under a certain key. When the stream is released under all the keys it was retained under, it is no longer refreshed during Streams.refresh() This method also closes a socket to the stream's node, if it was the last stream released on that node.

Returns:

Q.Streams.Stream:

release

(
  • publisherId
  • streamName
)
static

Releases a stream from being retained. See Streams.Stream.retain() This method also closes a socket to the stream's node, if it was the last stream released on that node.

Parameters:

remove

(
  • publisherId
  • streamName
  • callback
)
static

Closes a stream in the database, and marks it for removal unless it is required.

Parameters:

remove

(
  • callback
)

Closes a stream in the database, and marks it for removal unless it is required.

Parameters:

removePermission

(
  • permission
  • UNKNOWN
)

Parameters:

reopen

() static

Reopens a stream in the database that was previously closed, but not yet removed.

retain

(
  • publisherId
  • streamName
  • key
  • callback
)
Object static

Call this function to retain a particular stream. When a stream is retained, it is refreshed when Streams.refresh() or stream.refresh() are called. You can release the stream with stream.release(). This method also opens a socket to the stream's node, if one isn't already open.

Parameters:

  • publisherId String

    the publisher of the stream(s)

  • streamName String | Array

    can be a string or array of strings

  • key String

    the key under which to retain

  • callback Function

    optional callback for when stream(s) are retained

Returns:

Object:

returns Streams object for chaining with .get() or .related()

retain

(
  • key
)
Q.Streams.Stream

Retain the stream in the client under a certain key. Retained streams are refreshed during Streams.refresh()

Parameters:

Returns:

Q.Streams.Stream:

retainedByKey

() Boolean

Whether stream retained

Returns:

retainWith

(
  • key
)
Object

When a stream is retained, it is refreshed when Streams.refresh() or stream.refresh() are called. You can release it with stream.release(). Call this function in a chain before calling stream.related, etc. in order to set the key for retaining the streams those functions obtain.

Parameters:

Returns:

Object:

returns Streams object for chaining with .get() or .related()

save

(
  • [options]
)

Save a stream to the server

Parameters:

  • [options] Object optional

    A hash of options for the subsequent refresh. See Q.Streams.Stream.refresh

    • [onSave] Q.Event optional

      When the result of the server request comes back. The first parameter is any error that may have occurred. Note: the steam may not be refreshed yet by that point. You can return false from the handler to prevent the refresh, for whatever reason.

    • [onRefresh] Q.Event optional

      When the stream has been saved and refrshed.

set

(
  • fieldName
  • value
)

Set the value of a field, pending to be saved to the server with the stream May cause some validators to run and throw a validation exception on the value.

Parameters:

  • fieldName String
  • value Mixed

setAttribute

(
  • attributeName
  • value
)

Set the value of an attribute, pending to be saved to the server with the stream

Parameters:

  • attributeName String
  • value Mixed

subscribe

(
  • callback
)

Subscribe to a stream, so you get realtime messages and offline notifications.

Parameters:

  • callback Function

    receives (err, participant) as parameters

subscribe

(
  • publisherId
  • streamName
  • [callback]
  • [options]
)
static

Subscribe to a stream, to start getting offline notifications May call Streams.subscribe.onError if an error occurs.

Parameters:

  • publisherId String

    id of publisher which is publishing the stream

  • streamName String

    name of stream to join

  • [callback] Function optional

    receives (err, participant) as parameters

  • [options] Object optional

    optional object that can include:

    • [device] Bool optional

      Whether to subscribe device when user subscribed to some stream

testAdminLevel

(
  • level
)
Boolean

Test whether the user has enough access rights when it comes to administering the stream

Parameters:

  • level String

    One of the values in Streams.ADMIN_LEVEL

Returns:

Boolean:

Returns true if the user has at least this level of access

testReadLevel

(
  • level
)
Boolean

Test whether the user has enough access rights when it comes to reading from the stream

Parameters:

  • level String

    One of the values in Streams.READ_LEVEL

Returns:

Boolean:

Returns true if the user has at least this level of access

testWriteLevel

(
  • level
)
Boolean

Test whether the user has enough access rights when it comes to writing to the stream

Parameters:

  • level String

    One of the values in Streams.WRITE_LEVEL

Returns:

Boolean:

Returns true if the user has at least this level of access

unrelateFrom

(
  • fromPublisherId
  • fromStreamName
  • relationType
  • [callback]
)

Removes a relation from another stream to this stream

Parameters:

  • fromPublisherId String

    id of publisher which is publishing the stream

  • fromStreamName String

    name of stream which is being unrelated

  • relationType String

    the type of the relation, such as "parent" or "photo"

  • [callback] Function optional

    callback to call with the results First parameter is the error, the second one is an object of Streams.RelatedTo objects you can iterate over with Q.each

unrelateTo

(
  • toPublisherId
  • toStreamName
  • relationType
  • [callback]
)

Removes a relation from this stream to another stream

Parameters:

  • toPublisherId String

    id of publisher which is publishing the stream

  • toStreamName String

    name of stream which the being unrelated

  • relationType String

    the type of the relation, such as "parent" or "photo"

  • [callback] Function optional

    callback to call with the results First parameter is the error, the second one is an object of Streams.RelatedTo objects you can iterate over with Q.each

unsubscribe

(
  • publisherId
  • streamName
  • callback
)
static

Unsubscribe from a stream you previously subscribed to May call Stream.unsubscribe.onError if an error occurs.

Parameters:

unsubscribe

(
  • callback
)

Unsubscribe from a stream that you previously subscribed to

Parameters:

  • callback Function

    Receives (err, participant) as parameters

Events

beforeSet

static

Event factory for validation hooks that run when setting stream fields. Have the hooks throw a Q.Error on validation errors.

Event Payload:

  • streamType String

    type of the stream

  • [attributeName] String optional

    name of the field being set

beforeSet

static

Event factory for validation hooks that run when setting stream attributes. Have the hooks throw a Q.Error on validation errors.

Event Payload:

  • streamType String

    type of the stream

  • [attributeName] String optional

    name of the attribute being set

beforeSet

Event factory for validation hooks that run when setting stream fields. Have the hooks throw a Q.Error on validation errors.

beforeSetAttribute

Event factory for validation hooks that run when setting stream attributes Have the hooks throw a Q.Error on validation errors.

close.onError

Occurs when Stream.close encounters an error trying to close a stream

join.onError

Occurs when Stream.join encounters an error trying to join the stream

leave.onError

Occurs when Stream.leave encounters an error leave a stream

onAttribute

Returns Q.Event which occurs when attributes of the stream officially updated

Event Payload:

  • [attributeName] String optional

    name of the attribute to listen for, or "" for all

onAttribute

static

Returns Q.Event which occurs when attributes of the stream officially updated

Event Payload:

  • [publisherId] String optional

    id of publisher which is publishing the stream

  • [streamName] String optional

    name of stream which the message is posted to, "" for all

  • [attributeName] String optional

    name of the attribute to listen for, or "" for all

onClosed

static

Returns Q.Event which occurs when a stream has been closed (and perhaps has been marked for removal)

Event Payload:

  • publisherId String

    id of publisher which is publishing this stream

  • [streamName] String optional

    name of this stream

onClosed

Event factory for listening for changed stream fields based on name.

onConstruct

Returns Q.Event which occurs after a stream is constructed on the client side Generic callbacks can be assigend by setting type or mtype or both to ""

Event Payload:

  • publisherId String

    id of publisher which is publishing the stream

  • [streamName] String optional

    name of stream which is being constructed on the client side

onConstruct

Returns Q.Event that you can use update any of your stream representations. If you are already handling the Streams.Stream.onFieldChanged and Streams.Stream.onAttribute events, however, then you don't need to also add a handler to this event, because they are called during the refresh anyway.

Event Payload:

  • publisherId String

    id of publisher which is publishing the stream

  • [streamName] String optional

    name of stream which is being refreshed

onFieldChanged

static

Returns Q.Event which occurs when fields of the stream officially changed on the server, or was simulated by the client. Either way, you can use this opportunity to update your tools and other visual representations of the stream. Note that this event occurs before the stream object is finally updated, so you can compare the old values of the fields to the new ones. If you need to use the updated stream object, use setTimeout(callback, 0). Finally, if the field which changed is an "extend" field for that stream type, then its value will be null even if the real value is probably something else. You will need to call stream.refresh(callback) to load the stream from the server and in the callback you'll finally have the stream object you've been looking for.

Event Payload:

  • [publisherId] String optional

    id of publisher which is publishing the stream

  • [streamName] String optional

    name of stream which the message is posted to

  • [fieldName] String optional

    name of the field to listen for, or "" for all fields

onFieldChanged

Event factory for listening for changed stream fields based on name.

Event Payload:

  • field String

    can be "" to get triggered for on all fields

onMessage

static

Returns Q.Event that occurs after the system learns of a new message that was posted. The platform makes sure the ordinals come in the right order, for each stream. So you just have to handle the messages to update your tools, pages, etc. By the time this event happens, the platform has already taken any default actions for standard events such as "Streams/join", etc. so the stream and all caches are up-to-date, e.g. the participants include the newly joined participant, etc.

Event Payload:

  • [publisherId] String optional

    id of publisher which is publishing the stream

  • [streamName] String optional

    name of stream which the message is posted to

  • [messageType] String optional

    type of the message, or its ordinal, pass "" for all types

onMessage

Returns Q.Event that occurs after the system learns of a new message that was posted. The platform makes sure the ordinals come in the right order, for each stream. So you just have to handle the messages to update your tools, pages, etc. By the time this event happens, the platform has already taken any default actions for standard events such as "Streams/join", etc. so the stream and all caches are up-to-date, e.g. the participants include the newly joined participant, etc.

Event Payload:

  • [messageType] String optional

    type of the message, or its ordinal, pass "" for all types

onRelatedFrom

Event factory for listening to this stream being related to other streams

onRelatedFrom

static

Returns Q.Event which occurs when this stream was related to a category stream. You may want to retain that category stream on the client.

Event Payload:

  • publisherId String

    id of publisher which is publishing this stream

  • [streamName] String optional

    name of this stream

onRelatedTo

static

Returns Q.Event which occurs when another stream has been related to this stream. You may want to retain this stream on the client.

Event Payload:

  • publisherId String

    id of publisher which is publishing this stream

  • [streamName] String optional

    name of this stream

onRelatedTo

Event factory for listening to streams being related to this stream

onUnrelatedFrom

static

Returns Q.Event which occurs when this stream was unrelated to a category stream You may want to release that category stream on the client.

Event Payload:

  • publisherId String

    id of publisher which is publishing this stream

  • [streamName] String optional

    name of this stream

onUnrelatedFrom

Event factory for listening to changes in relations from this stream to others

onUnrelatedFrom

Event factory for listening to this stream becoming un-related to other streams

onUnrelatedTo

static

Returns Q.Event which occurs when another stream has been unrelated to this stream You may want to release this stream on the client.

Event Payload:

  • publisherId String

    id of publisher which is publishing this stream

  • [streamName] String optional

    name of this stream

onUnrelatedTo

Event factory for listening to other streams becoming un-related to this stream

onUnrelatedTo

Event factory for listening to changes in relations of other streams to this stream

onUpdated

Alias for onAttribute for backward compatibility

onUpdated

static

Alias for onAttribute for backward compatibility

Event Payload:

  • publisherId String

    id of publisher which is publishing the stream

  • [streamName] String optional

    name of stream which the message is posted to

  • [attributeName] String optional

    name of the attribute to listen for

onUpdatedRelateFrom

static

Returns Q.Event which occurs when this stream was related to a category stream

Event Payload:

  • publisherId String

    id of publisher which is publishing this stream

  • [streamName] String optional

    name of this stream

onUpdatedRelateTo

static

Returns Q.Event which occurs when another stream has been related to this stream

Event Payload:

  • publisherId String

    id of publisher which is publishing this stream

  • [streamName] String optional

    name of this stream

subscribe.onError

Occurs when Stream.subscribe encounters an error trying to subscribe to a stream

unsubscribe.onError

Occurs when Stream.unsubscribe encounters an error trying to unsubscribe from a stream