A basic Singularity client-side example

📘

OAuth Credentials Needed!

Remember Singularity uses OAuth2 credentials to connect and to request channel data. So make sure you review the "Registering an Application" and the "Connecting to a GameWisp Channel" sections of the docs if you haven't yet.

Singularity can be accessed by both client and server side applications. This minimal example was taken from the SocketIO-client-app example.

<!doctype html>
<html>
    <head>
        <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>

        <script>

            // Do not expose your secret! 
            var devCredentials = {
                key: "<OAuth Client ID>",
                secret: "<OAuth Client Secret>"
            };

            var socket = io("https://singularity.gamewisp.com");

            socket.on("connect", function(){

                socket.emit("authentication", 
                    {
                        key: devCredentials.key, 
                        secret: devCredentials.secret,
                    }
                );

                socket.on("authenticated", function(data) {
                    socket.emit("channel-connect", {
                       access_token: "<Channel OAuth Token>"
                    });

                    socket.on("app-channel-connected", function(response){
                        //...                    
                    });

                    socket.on("event-of-interest", function(response){
                        //...
                    });
                });
              	
              	//listen to the unauthorized event in case your client auth
              	//fails.
                socketClient.on('unauthorized', function(err){
                  console.log('Authentication error: ' + err.message);
                });
            });
        </script>
    </head>
    <body>
    </body>
</html>