0

I have the following code that queries and listens to comments. Each comment has a page property which stores the comment's page identifier. This identifier is the connection between the comment and its page on the website.

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

io.socket.get('/comment', {
  page: window.PAGE
}, function serverResponded (body, JWR) {
  console.log('Sails responded with: ', body)
  console.log('with headers: ', JWR.headers)
  console.log('and with status code: ', JWR.statusCode)
})

With the code above the browser gets informed about each new comment, even if it does not belong to the current page.

How can I achieve that the browser gets informed about new comments only if the created comment's page equals the current page (which is stored in window.PAGE)? Is it possible with the built-in websocket functionality or do I need to write an extension?

ideaboxer
  • 3,863
  • 8
  • 43
  • 62

1 Answers1

0

You can just overwrite the publishCreate callback in your comment model in api/models/comment.js.

Take a look at the sails reference http://sailsjs.org/documentation/reference/web-sockets/resourceful-pub-sub/publish-create or take a look at this, where you can find an example code Conditional publish events.

Community
  • 1
  • 1
ju_
  • 569
  • 1
  • 4
  • 17