sails.jssails.io.js

Using sails.io pubsub works with shortcuts, but not with RESTful record creation


I'm having an issue where I'm getting events from the sails resourceful pubsub when I create a record with the shortcut routes, but not with the RESTful routes.

In my client code, I have a sails socket that I listen to for the model:

io.socket.on('users', function(event){console.log(event);})

If I use a shortcut route to create a record (http://localhost:1337/users/create?name=test), I get the callback in the console as expected:

>{verb: "created", data: {…}, id: 44}

However, if I use the socket to post from the client, the callback never fires.

io.socket.post('/users', { name: 'test' });

The record is created in the DB, and even more confusing is that the Sails server log says its publishing the message:

silly: Published message to  sails_model_create_users :  { verb: 'created',
  data:
   { name: 'test',
     createdAt: '2017-10-09T02:58:18.218Z',
     updatedAt: '2017-10-09T02:58:18.218Z',
     id: 44 },
  id: 44 }

I'm the using generic blueprints, sails is v0.12.14. Any ideas what I'm doing wrong?


Solution

  • I figured out why I wasn't getting the events back on the pubsub. Sails excludes the socket that sent the req from the event. You can get the result from the post callback, but this breaks my nice dataflow. So I ended up using io.sails.connect() to create 2 sockets, one for the crud calls and one for the pubsib.