View our GitHub

Please visit sails-docs on GitHub to view documentation on your mobile device.

Edit Page

.publishCreate( data,[request] )


PublishCreate doesn't actually create anything. It simply publishes information about the creation of a model instance via websockets. PublishCreate is called automatically by the blueprint create action.

Description Accepted Data Types Required ?
1 Data to Send object Yes
2 Request Request object No

The default implementation of publishCreate only publishes messages to the firehose, and to sockets subscribed to the model class using the watch method. It also subscribes all sockets "watching" the model class to the new instance. The socket message to subscribers will include the following properties:

  • id - the id attribute of the new model instance
  • verb - "created" (a string)
  • data - an object-- the attributes and values of the new model instance


An object containing the attributes and values of the new model instance.


If this argument is included then the socket attached to that request will not receive the notification.

Example Usage


module.exports = {

  testSocket: function(req,res){

        var nameSent = req.param('name');

        if (nameSent && req.isSocket){

          User.create({name:nameSent}).exec(function created(err,newGuy){
            console.log('A new user called '' has been created');

        } else if (req.isSocket){

          console.log('User with socket id '' is now subscribed to the model class \'users\'.');

        } else {



    // Don't forget to handle your errors


<script type="text/javascript">
window.onload = function subscribeAndListen(){
    // When the document loads, send a request to users.testSocket
    // The controller code will subscribe you to the model 'users'

    // Listen for the event called 'user' emited by the publishCreate() method.
      if (obj.verb == 'created') {
         var data =;
         console.log('User '' has been created.');

function makeNew(){

    // Send the new users name to the 'testSocket' action on the 'users' controller


<div class="addButton" onClick="makeNew()">Click Me to add a new 'Walter' ! </div>

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.


We wrote a book!

Get early access to the book
with promotion code: mcneildoc

Get the Book