.message( models
,data
, [request
] )
Purpose
Publishes a custom message to a model’s subscribers.
Description | Accepted Data Types | Required ? | |
---|---|---|---|
1 | Record (or ID of record) to send message to | int , string , object |
Yes |
2 | Message payload | object |
Yes |
3 | Request | request object |
No |
message()
emits a socket message using the model identity as the event name. The message is broadcast to all sockets subscribed to the model instance via the .subscribe
model method.
The socket message is an object with the following properties:
- id - the
id
attribute of the model instance - verb -
"messaged"
(a string) - data - the message payload
data
Arbitrary data to send to the subscribed sockets.
request
If this argument is included then the socket attached to that request will not receive the notification.
Example
Find a user in the sails controller by name and send a socket message back to the client. This object can contain any data you want.
User.findOne({name: 'Bob'}).then(function(foundUser){
User.message(foundUser, {count: 12, hairColor: 'red'});
});
Is something missing?
If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails-docs repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.
Reference
-
Application
-
Blueprint API
-
Command Line Interface
-
Configuration
-
sails.config.*
-
sails.config.blueprints
-
sails.config.cors
-
sails.config.csrf
-
sails.config.bootstrap
-
sails.config.connections
-
sails.config.globals
-
sails.config.i18n
-
sails.config.http
-
sails.config.policies
-
sails.config.routes
-
sails.config.models
-
sails.config.log
-
sails.config.session
-
sails.config.sockets
-
sails.config.views
-
sails.config.*
-
Request (`req`)
-
req.options
-
req.accepts()
-
req.acceptedLanguages
-
req.accepted
-
req.acceptedCharsets
-
req.acceptsCharset()
-
req.acceptsLanguage()
-
req.body
-
req.allParams()
-
req.cookies
-
req.fresh
-
req.get()
-
req.host
-
req.file()
-
req.ip
-
req.headers
-
req.is()
-
req.ips
-
req.isSocket
-
req.param()
-
req.method
-
req.path
-
req.params
-
req.protocol
-
req.secure
-
req.query
-
req.subdomains
-
req.signedCookies
-
req.socket
-
req.url
-
req.wantsJSON
-
req.xhr
-
req.options
-
Response (`res`)
-
Waterline (ORM)
-
WebSockets