{"_id":"56a0bda13697d80d002ac607","project":"54774d9af3736008009e9e0e","user":"54e341c4ce1b4017000dffef","__v":30,"parentDoc":null,"category":{"_id":"56b098a43a5b810d00745e3f","pages":["56b09b3b135ec20d00baf946"],"project":"54774d9af3736008009e9e0e","__v":1,"version":"56a0bd9e3697d80d002ac5e7","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-02-02T11:53:08.831Z","from_sync":false,"order":14,"slug":"sockets","title":"Sockets"},"version":{"_id":"56a0bd9e3697d80d002ac5e7","project":"54774d9af3736008009e9e0e","__v":20,"createdAt":"2016-01-21T11:14:38.131Z","releaseDate":"2016-01-21T11:14:38.131Z","categories":["56a0bd9f3697d80d002ac5e8","56a0bd9f3697d80d002ac5e9","56a0bd9f3697d80d002ac5ea","56a0bd9f3697d80d002ac5eb","56b098a43a5b810d00745e3f","56b098da8f7a4f0d0029dd10","56d5a8ecf612b80b00fb69cd","570989853ab43c0e0072b2d6","571006aa10300c0e007f6133","5735a52431a73b1700887ca0","5744b0529e045c0e00b7a7a2","5744b1d0b56d431700d8a4fb","5744b1fb55d65a0e00b436e1","5744b218f9c7411700ce560e","5744b220f9c7411700ce560f","5744b2259e045c0e00b7a7a7","5744b22cf8b79f0e00de6a2d","5744b23155d65a0e00b436e4","5744b242c758290e00fbc235","5744b2569e045c0e00b7a7a8","5756a3f2bb92962900dafe3d","57ee23e81e42900e0014a42f","581c822c98676e0f00d240ef"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"Sockets","version_clean":"1.1.0","version":"1.1"},"updates":["55f972e430f2600d00f933e1"],"next":{"pages":[],"description":""},"createdAt":"2015-03-10T13:28:52.473Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":4,"body":"## Chapter content:\n1. [Trigger Sockets Overview](#trigger-sockets-overview)\n2. [Using Trigger Sockets](#using-trigger-sockets)\n3. [Sumary](#summary)\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Trigger Sockets Overview\"\n}\n[/block]\nApart from directly running Snippet Scripts, or using CodBoxes and Schedules, Trigger Sockets are another way of executing Scripts. Trigger Sockets execute a Script when a Data Object inside selected Data Class is created, updated or deleted (depends on \"signal\" field value). \nThis is how a Trigger Socket object can look:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{\\n    \\\"id\\\": 1, \\n    \\\"label\\\": \\\"get reviews\\\", \\n    \\\"signal\\\": \\\"post_create\\\",\\n    \\\"description\\\": \\\"this trigger will start a snippet script with an id = 1\\\",\\n    \\\"class\\\": \\\"books\\\", \\n    \\\"created_at\\\": \\\"2015-04-25T10:55:37.957096Z\\\", \\n    \\\"updated_at\\\": \\\"2015-04-25T10:55:37.957138Z\\\", \\n    \\\"script\\\": 1, \\n    \\\"links\\\": {\\n        \\\"script\\\": \\\"/v1.1/instances/bookstore/snippets/scripts/1/\\\", \\n        \\\"self\\\": \\\"/v1.1/instances/bookstore/triggers/1/\\\", \\n        \\\"traces\\\": \\\"/v1.1/instances/bookstore/triggers/1/traces/\\\", \\n        \\\"class\\\": \\\"/v1.1/instances/bookstore/classes/books/\\\"\\n    }\\n}\",\n      \"language\": \"json\"\n    }\n  ]\n}\n[/block]\nAnd these are the unique properties of a Trigger Socket:\n\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Property\",\n    \"h-1\": \"Description\",\n    \"0-0\": \"signal\",\n    \"0-1\": \"This property determines on what condition the Snippet Script should run. There are three possible values here:\\n* `post_create` - run Script when a Data Object is created\\n* `post_update` - run Script when a Data Object is updated\\n* `post_delete` - run Script when a Data Object is removed\",\n    \"1-0\": \"class\",\n    \"1-1\": \"Name of a data class. A Trigger will execute a Script when changes happen to a Data Object is this data class.\",\n    \"2-0\": \"script\",\n    \"2-1\": \"`id` of a Snippet Script that'll be executed by this trigger.\"\n  },\n  \"cols\": 2,\n  \"rows\": 3\n}\n[/block]\nSince we now know what Triggers are, we can now move to a real-life example.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Using Trigger Sockets\"\n}\n[/block]\nSo how can we use triggers to our advantage?\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"This tutorial assumes that you are familiar with [Data Classes](classes) and [Snippet Scripts](snippets-scripts) concepts.\"\n}\n[/block]\nLet's use our beloved books example. We've got a library application that people add books to. We'd like to get reviews for a book whenever someone adds a book to our library.\n\nWhat we need to do:\n1. Create a `book` Data Class\n2. Create a Snippet Script that'll get us books reviews\n3. Add a trigger that'll run when a new book Data Object is added to books Data Class\n4. Add a book Data Object and enjoy the results!\n\n## Create a books Data Class\n\nOur book Data Objects will have two custom fields which are `title` and `isbn`.\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"`isbn` stands for [International Standard Book Number](https://en.wikipedia.org/wiki/International_Standard_Book_Number). We'll need it for our Script to work.\"\n}\n[/block]\nIn order to be able to add our Data Objects, first create a Data Class that uses this schema:\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Name\",\n    \"h-1\": \"Type\",\n    \"0-0\": \"title\",\n    \"0-1\": \"string\",\n    \"1-0\": \"isbn\",\n    \"1-1\": \"string\"\n  },\n  \"cols\": 2,\n  \"rows\": 2\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"For details on adding a new Data Class, follow the guide in [Data Classes](classes-creating-a-class) chapter of this Developer Manual.\"\n}\n[/block]\n## Create a Snippet Script that'll get us books reviews\n\nNow it's time for the magic. Since we are lazy and don't want to write the reviews ourselves, we'll use [Goodreads API](https://www.goodreads.com/api/) to get them. What the source code below does is:\n1. Gets the isbn of the most recently added Data Object \n2. Goes to Goodreads and gets the reviews\n3. Returns the reviews widget for our book\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import json\\nimport requests\\n\\nisbn = ARGS['isbn']\\n\\ngoodreads_api = \\\"https://www.goodreads.com/book/isbn?format=json&isbn=\\\"+isbn+\\\"&user_id=42491463\\\"\\nr = requests.get(goodreads_api)\\nprint json.loads(r.content)[\\\"reviews_widget\\\"]\",\n      \"language\": \"python\"\n    },\n    {\n      \"code\": \"var request = require('request');\\n\\nvar goodreadsApi = 'https://www.goodreads.com/book/isbn?format=json&isbn=' + ARGS.isbn + '&user_id=42491463';\\n\\nrequest.get(goodreadsApi, function(err, res) {\\n  if (err) {console.log(err); return;}\\n  console.log(JSON.parse(body).reviews_widget);\\n  return;\\n});\",\n      \"language\": \"javascript\",\n      \"name\": \"Node.js\"\n    },\n    {\n      \"code\": \"# Coming soon!\",\n      \"language\": \"ruby\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"For details on adding a Snippet Script using the Dashboard, please follow the guide in [Snippet Scripts](doc:snippets-scripts#adding-a-new-script) chapter of this Developer Manual.\"\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"ARGS\",\n  \"body\": \"It is important to note that you can access the attributes of the Data Object that triggered the Script run. The attributes can be accessed by using the built in ARGS variable. For instance, in the example above we are accessing `isbn` attribute of a book Data Object which has just triggered the `get reviews` Script.\"\n}\n[/block]\n## Add a Trigger Socket\n\nThis that'll run when a new book Data Object is added to books Data Class\n\nNow what we'd like to do is to make our Snippet Script code run whenever someone adds a new book to our library (books data class). Enter Trigger Sockets! We can create a Trigger Socket that'll run the above Script when someone adds a Data Object to books Data Class. \n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/0UycDchmS4uMwwS93ueM_Add_trigger_01.png\",\n        \"Add_trigger_01.png\",\n        \"1279\",\n        \"653\",\n        \"#233f6a\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/cGqMEplpSZy9DdDSExVj_Add_trigger_02.png\",\n        \"Add_trigger_02.png\",\n        \"1279\",\n        \"657\",\n        \"#6c84a4\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nYou have added your trigger socket! Now lets check out how it works.\n\n## Add a book Data Object to see the Trigger Sockets in action!\nSince we now have all the elements in place, let's test it out by adding a book Data Object. We'll add \"Fight Club\" which has the isbn# `9780805062977`.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"curl -X POST \\\\\\n-H \\\"Content-type: application/json\\\" \\\\\\n-H \\\"X-API-KEY: ACCOUNT_KEY\\\" \\\\\\n-d '{\\\"title\\\": \\\"Fight Club\\\", \\\"isbn\\\": \\\"9780805062977\\\"}' \\\\\\n\\\"https://api.syncano.io/v1.1/instances/instance_name/classes/books/objects/\\\"\",\n      \"language\": \"curl\"\n    },\n    {\n      \"code\": \"import syncano\\nfrom syncano.models import Object\\n\\nsyncano.connect(api_key=\\\"Api_key\\\")\\n\\nObject.please.create(\\n  title=\\\"Fight Club\\\",\\n  isbn=\\\"9780805062977\\\",\\n  instance_name=\\\"INSTANCE_NAME\\\",\\n  class_name=\\\"books\\\"\\n)\",\n      \"language\": \"python\"\n    },\n    {\n      \"code\": \"var Syncano = require(\\\"syncano\\\");  // CommonJS\\nvar connection = Syncano({accountKey: \\\"ACCOUNT_KEY\\\"});\\nvar DataObject = connection.DataObject;\\n\\nvar book = {\\n  title: \\\"The Nightingale\\\", \\n  isbn: \\\"0312577222\\\",\\n  instanceName: \\\"INSTANCE_NAME\\\",\\n  className: \\\"CLASS_NAME\\\"\\n};\\n\\nDataObject.please().create(book).then(callback);\",\n      \"language\": \"javascript\"\n    },\n    {\n      \"code\": \"// Not supported\",\n      \"language\": \"java\",\n      \"name\": \"Android\"\n    },\n    {\n      \"code\": \":::at:::interface Book : SCDataObject\\[email protected] (nonatomic,copy) NSString *title;\\[email protected] (nonatomic,copy) NSString *isbn;\\[email protected]\\n  \\nBook *book = [Book new];\\nbook.title = @\\\"Fight Club\\\";\\nbook.isbn = @\\\"9780805062977\\\";\\n[book saveWithCompletionBlock:^(NSError *error) {\\n  //handle error\\n}];\",\n      \"language\": \"objectivec\"\n    },\n    {\n      \"code\": \"class Book : SCDataObject {\\n  var title = \\\"\\\"\\n  var isbn = \\\"\\\"\\n}\\n\\nlet book = Book()\\nbook.title = \\\"Fight Club\\\"\\nbook.isbn = \\\"9780805062977\\\"\\nbook.saveWithCompletionBlock { error in\\n  //handle error\\n}\",\n      \"language\": \"javascript\",\n      \"name\": \"Swift\"\n    },\n    {\n      \"code\": \"# Coming soon!\",\n      \"language\": \"ruby\"\n    },\n    {\n      \"code\": \"// NOT AVAILABLE\",\n      \"language\": \"csharp\",\n      \"name\": \"Unity\"\n    }\n  ]\n}\n[/block]\nThis Data Object creation triggered our Snippet Script execution. To see the results we could make a call to the [Trigger Sockets Traces endpoint](traces), get the latest ID and get the trace. This is how the response would look:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{\\n    \\\"id\\\": 1, \\n    \\\"status\\\": \\\"success\\\", \\n    \\\"executed_at\\\": \\\"2015-04-25T11:01:18.595539Z\\\", \\n    \\\"duration\\\": 824, \\n    \\\"result\\\": \\\"<style> #goodreads-widget { font-family: georgia, serif; padding: 18px 0; width:565px; } #goodreads-widget h1 { font-weight:normal; font-size: 16px; border-bottom: 1px solid #BBB596; margin-bottom: 0; } #goodreads-widget a { text-decoration: none; color:#660; } iframe{ background-color: #fff; } #goodreads-widget a:hover { text-decoration: underline; } #goodreads-widget a:active { color:#660; } #gr_footer { width: 100%; border-top: 1px solid #BBB596; text-align: right; } #goodreads-widget .gr_branding{ color: #382110; font-size: 11px; text-decoration: none; font-family: \\\\\\\"Helvetica Neue\\\\\\\", Helvetica, Arial, sans-serif; } </style> <div id=\\\\\\\"goodreads-widget\\\\\\\"> <div id=\\\\\\\"gr_header\\\\\\\"><h1><a href=\\\\\\\"https://www.goodreads.com/book/show/21853621-the-nightingale\\\\\\\">The Nightingale Reviews</a></h1></div> <iframe id=\\\\\\\"the_iframe\\\\\\\" src=\\\\\\\"https://www.goodreads.com/api/reviews_widget_iframe?did=DEVELOPER_ID&amp;format=html&amp;isbn=0312577222&amp;links=660&amp;review_back=fff&amp;stars=000&amp;text=000\\\\\\\" width=\\\\\\\"565\\\\\\\" height=\\\\\\\"400\\\\\\\" frameborder=\\\\\\\"0\\\\\\\"></iframe> <div id=\\\\\\\"gr_footer\\\\\\\"> <a class=\\\\\\\"gr_branding\\\\\\\" target=\\\\\\\"_blank\\\\\\\" href=\\\\\\\"https://www.goodreads.com/book/show/21853621-the-nightingale?utm_medium=api&amp;utm_source=reviews_widget\\\\\\\">Reviews from Goodreads.com</a> </div> </div>\\\", \\n    \\\"links\\\": {\\n        \\\"self\\\": \\\"/v1.1/instances/bookstore/triggers/1/traces/1/\\\"\\n    }\\n}\",\n      \"language\": \"json\"\n    }\n  ]\n}\n[/block]\nWhat do have in the `result` field? It's a nice Goodreads reviews iframe that we can embed on our website just like we did below:\n[block:html]\n{\n  \"html\": \"<style> \\n  #goodreads-widget { \\n    font-family: georgia, serif; \\n    padding: 18px 0; \\n    width:565px; \\n  } \\n  #goodreads-widget h1 { \\n    font-weight:normal; \\n    font-size: 16px; \\n    border-bottom: 1px solid #BBB596; \\n    margin-bottom: 0; \\n  } \\n  #goodreads-widget a { \\n    text-decoration: none; \\n    color:#660; \\n  } \\n  iframe { \\n    background-color: #fff; \\n  } \\n  #goodreads-widget a:hover { \\n    text-decoration: underline; \\n  } \\n  #goodreads-widget a:active { \\n    color:#660; \\n  } \\n  #gr_footer { \\n    width: 100%; \\n    border-top: 1px solid #BBB596; \\n    text-align: right; \\n  } #goodreads-widget \\n  .gr_branding{ \\n    color: #382110; \\n    font-size: 11px; \\n    text-decoration: none; \\n    font-family: \\\"Helvetica Neue\\\", Helvetica, Arial, sans-serif; \\n  } \\n</style> \\n<div id=\\\"goodreads-widget\\\"> \\n  <div id=\\\"gr_header\\\">\\n    <h1><a href=\\\"https://www.goodreads.com/book/show/21853621-the-nightingale\\\">The Nightingale Reviews</a></h1>\\n  </div> \\n  <iframe id=\\\"the_iframe\\\" src=\\\"https://www.goodreads.com/api/reviews_widget_iframe?did=DEVELOPER_ID&amp;format=html&amp;isbn=0312577222&amp;links=660&amp;review_back=fff&amp;stars=000&amp;text=000\\\" width=\\\"565\\\" height=\\\"400\\\" frameborder=\\\"0\\\"></iframe> \\n  <div id=\\\"gr_footer\\\"> \\n    <a class=\\\"gr_branding\\\" target=\\\"_blank\\\" href=\\\"https://www.goodreads.com/book/show/21853621-the-nightingale?utm_medium=api&amp;utm_source=reviews_widget\\\">Reviews from Goodreads.com</a> \\n  </div> \\n</div>\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"META Data\"\n}\n[/block]\nTriggers also give us META data that allow us to track what is happening with the trigger and react on it through the assigned script.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{ \\n  instance: 'spring-frog-7062',\\n  executed_by: 'trigger',\\n  executor: 8,\\n  signal: 'create',\\n  changes: [ 'updated_at', 'to', 'image', 'message', 'revision' ],\\n  event: {\\n    source: 'dataobject',\\n    class: 'newsletter'\\n  } \\n}\",\n      \"language\": \"json\"\n    }\n  ]\n}\n[/block]\n\n[block:parameters]\n{\n  \"data\": {\n    \"0-0\": \"instance\",\n    \"0-1\": \"instance name\",\n    \"h-0\": \"key\",\n    \"h-1\": \"meaning\",\n    \"1-0\": \"executed_by\",\n    \"1-1\": \"socket which has run this action\",\n    \"2-0\": \"executor\",\n    \"2-1\": \"socket id which has run this action\",\n    \"3-0\": \"signal\",\n    \"3-1\": \"on which signal has trigger run\",\n    \"4-0\": \"changes\",\n    \"4-1\": \"fields name that has changed in Data Class Data Objects\",\n    \"5-0\": \"event\",\n    \"5-1\": \"source: signal source\\nclass: Data Class name where something has changed\"\n  },\n  \"cols\": 2,\n  \"rows\": 6\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Summary\"\n}\n[/block]\n\nWe've learned what Trigger Sockets are and how to use them in a real life. You can learn about other Snippet Scripts execution methods like [Script Endpoints](doc:endpoints-scripts) or [Schedule Sockets](doc:schedules). If you already know how these work, [Channel Sockets](doc:realtime-communication) chapter is the next recommended read.\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Learn More\",\n  \"body\": \"For more details about the available methods for Trigger Sockets, visit the [Trigger Sockets API reference](http://docs.syncano.com/v0.1.1/docs/triggers-list).\"\n}\n[/block]","excerpt":"In this chapter you will read about:\n* What is a Trigger and what are its properties\n* How to use a Trigger in a real-life example\n* Possible usage examples","slug":"triggers","type":"basic","title":"Trigger Sockets"}

Trigger Sockets

In this chapter you will read about: * What is a Trigger and what are its properties * How to use a Trigger in a real-life example * Possible usage examples

## Chapter content: 1. [Trigger Sockets Overview](#trigger-sockets-overview) 2. [Using Trigger Sockets](#using-trigger-sockets) 3. [Sumary](#summary) [block:api-header] { "type": "basic", "title": "Trigger Sockets Overview" } [/block] Apart from directly running Snippet Scripts, or using CodBoxes and Schedules, Trigger Sockets are another way of executing Scripts. Trigger Sockets execute a Script when a Data Object inside selected Data Class is created, updated or deleted (depends on "signal" field value). This is how a Trigger Socket object can look: [block:code] { "codes": [ { "code": "{\n \"id\": 1, \n \"label\": \"get reviews\", \n \"signal\": \"post_create\",\n \"description\": \"this trigger will start a snippet script with an id = 1\",\n \"class\": \"books\", \n \"created_at\": \"2015-04-25T10:55:37.957096Z\", \n \"updated_at\": \"2015-04-25T10:55:37.957138Z\", \n \"script\": 1, \n \"links\": {\n \"script\": \"/v1.1/instances/bookstore/snippets/scripts/1/\", \n \"self\": \"/v1.1/instances/bookstore/triggers/1/\", \n \"traces\": \"/v1.1/instances/bookstore/triggers/1/traces/\", \n \"class\": \"/v1.1/instances/bookstore/classes/books/\"\n }\n}", "language": "json" } ] } [/block] And these are the unique properties of a Trigger Socket: [block:parameters] { "data": { "h-0": "Property", "h-1": "Description", "0-0": "signal", "0-1": "This property determines on what condition the Snippet Script should run. There are three possible values here:\n* `post_create` - run Script when a Data Object is created\n* `post_update` - run Script when a Data Object is updated\n* `post_delete` - run Script when a Data Object is removed", "1-0": "class", "1-1": "Name of a data class. A Trigger will execute a Script when changes happen to a Data Object is this data class.", "2-0": "script", "2-1": "`id` of a Snippet Script that'll be executed by this trigger." }, "cols": 2, "rows": 3 } [/block] Since we now know what Triggers are, we can now move to a real-life example. [block:api-header] { "type": "basic", "title": "Using Trigger Sockets" } [/block] So how can we use triggers to our advantage? [block:callout] { "type": "info", "body": "This tutorial assumes that you are familiar with [Data Classes](classes) and [Snippet Scripts](snippets-scripts) concepts." } [/block] Let's use our beloved books example. We've got a library application that people add books to. We'd like to get reviews for a book whenever someone adds a book to our library. What we need to do: 1. Create a `book` Data Class 2. Create a Snippet Script that'll get us books reviews 3. Add a trigger that'll run when a new book Data Object is added to books Data Class 4. Add a book Data Object and enjoy the results! ## Create a books Data Class Our book Data Objects will have two custom fields which are `title` and `isbn`. [block:callout] { "type": "info", "body": "`isbn` stands for [International Standard Book Number](https://en.wikipedia.org/wiki/International_Standard_Book_Number). We'll need it for our Script to work." } [/block] In order to be able to add our Data Objects, first create a Data Class that uses this schema: [block:parameters] { "data": { "h-0": "Name", "h-1": "Type", "0-0": "title", "0-1": "string", "1-0": "isbn", "1-1": "string" }, "cols": 2, "rows": 2 } [/block] [block:callout] { "type": "info", "body": "For details on adding a new Data Class, follow the guide in [Data Classes](classes-creating-a-class) chapter of this Developer Manual." } [/block] ## Create a Snippet Script that'll get us books reviews Now it's time for the magic. Since we are lazy and don't want to write the reviews ourselves, we'll use [Goodreads API](https://www.goodreads.com/api/) to get them. What the source code below does is: 1. Gets the isbn of the most recently added Data Object 2. Goes to Goodreads and gets the reviews 3. Returns the reviews widget for our book [block:code] { "codes": [ { "code": "import json\nimport requests\n\nisbn = ARGS['isbn']\n\ngoodreads_api = \"https://www.goodreads.com/book/isbn?format=json&isbn=\"+isbn+\"&user_id=42491463\"\nr = requests.get(goodreads_api)\nprint json.loads(r.content)[\"reviews_widget\"]", "language": "python" }, { "code": "var request = require('request');\n\nvar goodreadsApi = 'https://www.goodreads.com/book/isbn?format=json&isbn=' + ARGS.isbn + '&user_id=42491463';\n\nrequest.get(goodreadsApi, function(err, res) {\n if (err) {console.log(err); return;}\n console.log(JSON.parse(body).reviews_widget);\n return;\n});", "language": "javascript", "name": "Node.js" }, { "code": "# Coming soon!", "language": "ruby" } ] } [/block] [block:callout] { "type": "info", "body": "For details on adding a Snippet Script using the Dashboard, please follow the guide in [Snippet Scripts](doc:snippets-scripts#adding-a-new-script) chapter of this Developer Manual." } [/block] [block:callout] { "type": "info", "title": "ARGS", "body": "It is important to note that you can access the attributes of the Data Object that triggered the Script run. The attributes can be accessed by using the built in ARGS variable. For instance, in the example above we are accessing `isbn` attribute of a book Data Object which has just triggered the `get reviews` Script." } [/block] ## Add a Trigger Socket This that'll run when a new book Data Object is added to books Data Class Now what we'd like to do is to make our Snippet Script code run whenever someone adds a new book to our library (books data class). Enter Trigger Sockets! We can create a Trigger Socket that'll run the above Script when someone adds a Data Object to books Data Class. [block:image] { "images": [ { "image": [ "https://files.readme.io/0UycDchmS4uMwwS93ueM_Add_trigger_01.png", "Add_trigger_01.png", "1279", "653", "#233f6a", "" ] } ] } [/block] [block:image] { "images": [ { "image": [ "https://files.readme.io/cGqMEplpSZy9DdDSExVj_Add_trigger_02.png", "Add_trigger_02.png", "1279", "657", "#6c84a4", "" ] } ] } [/block] You have added your trigger socket! Now lets check out how it works. ## Add a book Data Object to see the Trigger Sockets in action! Since we now have all the elements in place, let's test it out by adding a book Data Object. We'll add "Fight Club" which has the isbn# `9780805062977`. [block:code] { "codes": [ { "code": "curl -X POST \\\n-H \"Content-type: application/json\" \\\n-H \"X-API-KEY: ACCOUNT_KEY\" \\\n-d '{\"title\": \"Fight Club\", \"isbn\": \"9780805062977\"}' \\\n\"https://api.syncano.io/v1.1/instances/instance_name/classes/books/objects/\"", "language": "curl" }, { "code": "import syncano\nfrom syncano.models import Object\n\nsyncano.connect(api_key=\"Api_key\")\n\nObject.please.create(\n title=\"Fight Club\",\n isbn=\"9780805062977\",\n instance_name=\"INSTANCE_NAME\",\n class_name=\"books\"\n)", "language": "python" }, { "code": "var Syncano = require(\"syncano\"); // CommonJS\nvar connection = Syncano({accountKey: \"ACCOUNT_KEY\"});\nvar DataObject = connection.DataObject;\n\nvar book = {\n title: \"The Nightingale\", \n isbn: \"0312577222\",\n instanceName: \"INSTANCE_NAME\",\n className: \"CLASS_NAME\"\n};\n\nDataObject.please().create(book).then(callback);", "language": "javascript" }, { "code": "// Not supported", "language": "java", "name": "Android" }, { "code": "@interface Book : SCDataObject\[email protected] (nonatomic,copy) NSString *title;\[email protected] (nonatomic,copy) NSString *isbn;\[email protected]\n \nBook *book = [Book new];\nbook.title = @\"Fight Club\";\nbook.isbn = @\"9780805062977\";\n[book saveWithCompletionBlock:^(NSError *error) {\n //handle error\n}];", "language": "objectivec" }, { "code": "class Book : SCDataObject {\n var title = \"\"\n var isbn = \"\"\n}\n\nlet book = Book()\nbook.title = \"Fight Club\"\nbook.isbn = \"9780805062977\"\nbook.saveWithCompletionBlock { error in\n //handle error\n}", "language": "javascript", "name": "Swift" }, { "code": "# Coming soon!", "language": "ruby" }, { "code": "// NOT AVAILABLE", "language": "csharp", "name": "Unity" } ] } [/block] This Data Object creation triggered our Snippet Script execution. To see the results we could make a call to the [Trigger Sockets Traces endpoint](traces), get the latest ID and get the trace. This is how the response would look: [block:code] { "codes": [ { "code": "{\n \"id\": 1, \n \"status\": \"success\", \n \"executed_at\": \"2015-04-25T11:01:18.595539Z\", \n \"duration\": 824, \n \"result\": \"<style> #goodreads-widget { font-family: georgia, serif; padding: 18px 0; width:565px; } #goodreads-widget h1 { font-weight:normal; font-size: 16px; border-bottom: 1px solid #BBB596; margin-bottom: 0; } #goodreads-widget a { text-decoration: none; color:#660; } iframe{ background-color: #fff; } #goodreads-widget a:hover { text-decoration: underline; } #goodreads-widget a:active { color:#660; } #gr_footer { width: 100%; border-top: 1px solid #BBB596; text-align: right; } #goodreads-widget .gr_branding{ color: #382110; font-size: 11px; text-decoration: none; font-family: \\\"Helvetica Neue\\\", Helvetica, Arial, sans-serif; } </style> <div id=\\\"goodreads-widget\\\"> <div id=\\\"gr_header\\\"><h1><a href=\\\"https://www.goodreads.com/book/show/21853621-the-nightingale\\\">The Nightingale Reviews</a></h1></div> <iframe id=\\\"the_iframe\\\" src=\\\"https://www.goodreads.com/api/reviews_widget_iframe?did=DEVELOPER_ID&amp;format=html&amp;isbn=0312577222&amp;links=660&amp;review_back=fff&amp;stars=000&amp;text=000\\\" width=\\\"565\\\" height=\\\"400\\\" frameborder=\\\"0\\\"></iframe> <div id=\\\"gr_footer\\\"> <a class=\\\"gr_branding\\\" target=\\\"_blank\\\" href=\\\"https://www.goodreads.com/book/show/21853621-the-nightingale?utm_medium=api&amp;utm_source=reviews_widget\\\">Reviews from Goodreads.com</a> </div> </div>\", \n \"links\": {\n \"self\": \"/v1.1/instances/bookstore/triggers/1/traces/1/\"\n }\n}", "language": "json" } ] } [/block] What do have in the `result` field? It's a nice Goodreads reviews iframe that we can embed on our website just like we did below: [block:html] { "html": "<style> \n #goodreads-widget { \n font-family: georgia, serif; \n padding: 18px 0; \n width:565px; \n } \n #goodreads-widget h1 { \n font-weight:normal; \n font-size: 16px; \n border-bottom: 1px solid #BBB596; \n margin-bottom: 0; \n } \n #goodreads-widget a { \n text-decoration: none; \n color:#660; \n } \n iframe { \n background-color: #fff; \n } \n #goodreads-widget a:hover { \n text-decoration: underline; \n } \n #goodreads-widget a:active { \n color:#660; \n } \n #gr_footer { \n width: 100%; \n border-top: 1px solid #BBB596; \n text-align: right; \n } #goodreads-widget \n .gr_branding{ \n color: #382110; \n font-size: 11px; \n text-decoration: none; \n font-family: \"Helvetica Neue\", Helvetica, Arial, sans-serif; \n } \n</style> \n<div id=\"goodreads-widget\"> \n <div id=\"gr_header\">\n <h1><a href=\"https://www.goodreads.com/book/show/21853621-the-nightingale\">The Nightingale Reviews</a></h1>\n </div> \n <iframe id=\"the_iframe\" src=\"https://www.goodreads.com/api/reviews_widget_iframe?did=DEVELOPER_ID&amp;format=html&amp;isbn=0312577222&amp;links=660&amp;review_back=fff&amp;stars=000&amp;text=000\" width=\"565\" height=\"400\" frameborder=\"0\"></iframe> \n <div id=\"gr_footer\"> \n <a class=\"gr_branding\" target=\"_blank\" href=\"https://www.goodreads.com/book/show/21853621-the-nightingale?utm_medium=api&amp;utm_source=reviews_widget\">Reviews from Goodreads.com</a> \n </div> \n</div>" } [/block] [block:api-header] { "type": "basic", "title": "META Data" } [/block] Triggers also give us META data that allow us to track what is happening with the trigger and react on it through the assigned script. [block:code] { "codes": [ { "code": "{ \n instance: 'spring-frog-7062',\n executed_by: 'trigger',\n executor: 8,\n signal: 'create',\n changes: [ 'updated_at', 'to', 'image', 'message', 'revision' ],\n event: {\n source: 'dataobject',\n class: 'newsletter'\n } \n}", "language": "json" } ] } [/block] [block:parameters] { "data": { "0-0": "instance", "0-1": "instance name", "h-0": "key", "h-1": "meaning", "1-0": "executed_by", "1-1": "socket which has run this action", "2-0": "executor", "2-1": "socket id which has run this action", "3-0": "signal", "3-1": "on which signal has trigger run", "4-0": "changes", "4-1": "fields name that has changed in Data Class Data Objects", "5-0": "event", "5-1": "source: signal source\nclass: Data Class name where something has changed" }, "cols": 2, "rows": 6 } [/block] [block:api-header] { "type": "basic", "title": "Summary" } [/block] We've learned what Trigger Sockets are and how to use them in a real life. You can learn about other Snippet Scripts execution methods like [Script Endpoints](doc:endpoints-scripts) or [Schedule Sockets](doc:schedules). If you already know how these work, [Channel Sockets](doc:realtime-communication) chapter is the next recommended read. [block:callout] { "type": "info", "title": "Learn More", "body": "For more details about the available methods for Trigger Sockets, visit the [Trigger Sockets API reference](http://docs.syncano.com/v0.1.1/docs/triggers-list)." } [/block]