{"_id":"56ad35e23fc1020d009776e3","__v":4,"project":"56acd71213ac890d001c3c7a","parentDoc":null,"version":{"_id":"56acd71213ac890d001c3c7d","project":"56acd71213ac890d001c3c7a","__v":15,"createdAt":"2016-01-30T15:30:26.928Z","releaseDate":"2016-01-30T15:30:26.928Z","categories":["56acd71313ac890d001c3c7e","56acd8f113ac890d001c3c81","56acd8f95ac4060d0027865e","56acd93a13ac890d001c3c82","56ad20660ab3c00d00ce3347","56ad356a2a7860170013f714","56ad47eb0ab3c00d00ce334f","56afd523bc304a0d00ace1df","56b29a019621f20d00efb37e","56b657e11bc6970d009feee7","5845cc3c39950c1b002afe77","5845cde163c11b250037967e","5846efd45d064323007b17b4","588604da4674e32300efd160","588627882393d50f00f1322c"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"category":{"_id":"56ad356a2a7860170013f714","__v":5,"version":"56acd71213ac890d001c3c7d","pages":["56ad35e23fc1020d009776e3","56ad37781a7ee417009ced1d","56ad3859693f0a0d0085f0cd","56ad38d71c09150d00a183ad","56ad4721cdbb3d170064eaf9"],"project":"56acd71213ac890d001c3c7a","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-01-30T22:12:58.236Z","from_sync":false,"order":10,"slug":"singularity-on-demand-events","title":"Singularity: On-Demand Events"},"githubsync":"","user":"56acd6caa040860d00ac94a2","updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-01-30T22:14:58.234Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"settings":"","results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":0,"body":"On-demand events are simply event listeners on the API that respond to events fired by your application.\n\nOn-demand events are the correct approach when you need to query the API directly for information, but don't want to resort to a full-blown RESTful API method. \n\nFor example, if you need to determine whether or not a user in a Twitch chat room is a GameWisp subscriber, you would use the [channel-subscribers](https://gamewisp.readme.io/docs/channel-subscribers) on-demand event.\n\nAll on-demand events require the following structure using Socket.IO:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"socket.emit('event-name', {\\n    access_token: \\\"channel OAuth2 access token\\\"\\n});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nTo retrieve a response from an on-demand event you will need to listen to the appropriate event. For example, you can use the [channel-connect](https://gamewisp.readme.io/docs/channel-connect) on-demand event to connect to a GameWisp channel, but you will not receive a response from that event unless you ensure that your app is listening for the app-channel-connected event. All on-demand events have listeners that use the app-* naming convention. For example:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"socket.emit('channel-subscribers', {\\n    access_token: \\\"channel OAuth2 access token\\\"\\n});\\n\\nsocket.on('app-channel-subscribers', function(data){\\n\\t//Do things with the data object here...\\n});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nOne of the primary purposes of the on-demand events is to allow an application to synchronize its state with GameWisp in the event of an application restart or when authorizing a new channel for the first time. \n\nFor example, after successfully connecting to a channel using [channel-connect](https://gamewisp.readme.io/docs/channel-connect) an app can (and should) get the current subscribers for the channel by using the [channel-subscribers](https://gamewisp.readme.io/docs/channel-subscribers) event.","excerpt":"Useful when an application needs to query the API, but doesn't want to resort to a RESTful API method.","slug":"on-demand-event-basics","type":"basic","title":"On-Demand Event Basics"}

On-Demand Event Basics

Useful when an application needs to query the API, but doesn't want to resort to a RESTful API method.

On-demand events are simply event listeners on the API that respond to events fired by your application. On-demand events are the correct approach when you need to query the API directly for information, but don't want to resort to a full-blown RESTful API method. For example, if you need to determine whether or not a user in a Twitch chat room is a GameWisp subscriber, you would use the [channel-subscribers](https://gamewisp.readme.io/docs/channel-subscribers) on-demand event. All on-demand events require the following structure using Socket.IO: [block:code] { "codes": [ { "code": "socket.emit('event-name', {\n access_token: \"channel OAuth2 access token\"\n});", "language": "javascript" } ] } [/block] To retrieve a response from an on-demand event you will need to listen to the appropriate event. For example, you can use the [channel-connect](https://gamewisp.readme.io/docs/channel-connect) on-demand event to connect to a GameWisp channel, but you will not receive a response from that event unless you ensure that your app is listening for the app-channel-connected event. All on-demand events have listeners that use the app-* naming convention. For example: [block:code] { "codes": [ { "code": "socket.emit('channel-subscribers', {\n access_token: \"channel OAuth2 access token\"\n});\n\nsocket.on('app-channel-subscribers', function(data){\n\t//Do things with the data object here...\n});", "language": "javascript" } ] } [/block] One of the primary purposes of the on-demand events is to allow an application to synchronize its state with GameWisp in the event of an application restart or when authorizing a new channel for the first time. For example, after successfully connecting to a channel using [channel-connect](https://gamewisp.readme.io/docs/channel-connect) an app can (and should) get the current subscribers for the channel by using the [channel-subscribers](https://gamewisp.readme.io/docs/channel-subscribers) event.