PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/reference/websockets/sails.io.js/io.socket.on.md

https://github.com/balderdashy/sails-docs
Markdown | 86 lines | 56 code | 30 blank | 0 comment | 0 complexity | b2c479a5ab315b47e98e2688369fe5e2 MD5 | raw file
  1. # io.socket.on()
  2. Starts listening for server-sent events from Sails with the specified `eventIdentity`. Will trigger the provided callback function when a matching event is received.
  3. ### Usage
  4. ```js
  5. io.socket.on(eventIdentity, function (msg) {
  6. // ...
  7. });
  8. ```
  9. | | Argument | Type | Details |
  10. |---|------------|:------------:|---------|
  11. | 1 | `eventIdentity` | ((string)) | The unique identity of a server-sent event, e.g. "recipe"
  12. | 2 | `callback` | ((function)) | Will be called when the server emits a message to this socket.
  13. ##### Callback
  14. | | Argument | Type | Details |
  15. |---|-----------|:------------:|---------|
  16. | 1 | `msg` | ((object)) | Message sent from the Sails server
  17. Note that the callback will NEVER trigger until one of your back-end controllers, models, services, etc. sends a message to this socket. Typically that is achieved one of the following ways:
  18. ###### Resourceful Pubsub Methods
  19. + server publishes a message about a record to which this socket is subscribed (see [Model.publishUpdate()](), [Model.publishDestroy()](), and [Model.subscribe()]())
  20. + server publishes a message informing all permitted watcher sockets that a new record has been created in the model with the same identity as `eventIdentity` (see [Model.publishCreate()]() and [Model.watch()]())
  21. ###### Low-Level Socket Methods
  22. + server emits a message to all known sockets (see [sails.sockets.blast()]())
  23. + server emits a message directly to this socket (`io.socket`) using its unique id (see [sails.sockets.emit()]())
  24. + server [broadcasts]() to a room in which this socket (`io.socket`) has been allowed to [join]() (remember that a socket only stays subscribed as long as it is connected-- i.e. as long as the browser tab is open)
  25. ### Example
  26. Listen for new orders and updates to existing orders:
  27. ```javascript
  28. io.socket.on('order', function onServerSentEvent (msg) {
  29. // msg => {...whatever the server published/emitted...}
  30. });
  31. ```
  32. ##### Another example, this time using Angular:
  33. > Note that this Angular example assumes the backend calls `publishCreate()` at some point.
  34. ```javascript
  35. angular.module('cafeteria').controller('CheckoutCtrl', function ($scope) {
  36. $scope.orders = $scope.orders || [];
  37. if (!io.socket.alreadyListeningToOrders) {
  38. io.socket.alreadyListeningToOrders = true;
  39. io.socket.on('order', function onServerSentEvent (msg) {
  40. // Let's see what the server has to say...
  41. switch(msg.verb) {
  42. case 'created':
  43. $scope.orders.push(msg.data); // (add the new order to the DOM)
  44. $scope.$apply(); // (re-render)
  45. break;
  46. default: return; // ignore any unrecognized messages
  47. }
  48. });
  49. }
  50. });
  51. ```
  52. ### Notes
  53. >+ When listening for resourceful pubsub calls, the `eventIdentity` is the same as the identity of the calling model (e.g. if you have a model "UserComment", the identity is "usercomment".)
  54. >+ For context-- these types of server-sent events are sometimes referred to as ["comet"](http://en.wikipedia.org/wiki/Comet_(programming)) messages.
  55. <docmeta name="uniqueID" value="socketon682488">
  56. <docmeta name="displayName" value="io.socket.on()">