JavaScript API

The JavaScript client API allows browser-based web applications to send and receive real-time messages via the PushFYI server. The JavaScript API uses HTML5 WebSocket interface to connect to PushFYI and exchange real-time messages.

The JavaScript API has 3 main functions in addition to 2 helper functions. In this documentation we will examine the API usage and some of the best practices.

init()

The init method allows a browser based client to connect to PushFYI WebSocket gateway to send and receive real-time messages. It is important for a client application to first connect to PushFYI prior to sending or receiving any real-time messages. Hence, this must be the first method that your client application will call in order to use PushFYI real-time transport.

This PushFYI JavaScript init method is invoked after creating pushfyi object constructor.

Parameter List:

SN parameters note
1 appsecret This parameter identifies your App when it connects to PushFYI. Since PushFYI can serve real-time messages to many apps simultaneously. This field allows PushFYI to distinguish between Apps, so that real-time messages from one App do not mix up with another App. The value of appsecret parameter is in UUID format. e.g. 123e4567-e89b-12d3-a456-426655440000
2 address URL of your pushfyi instance. e.g. ws://10.0.0.101 for plain text connection and wss://10.0.0.101 for secure connection.

Code Sample:


<html>
    <head>
        <title>Pushfyi JS Test Application</title>
    </head>
    <body>
        <script src="pushfyi.js" type="text/javascript"></script>
        <script>
            var pushfyi = new Pushfyi("<your appsecret key>", "ws://<your pushfyi ip address>");
            pushfyi.init();
        </script>
        
    </body>
</html>

subscribe()

The subscribe method allows a browser based client to register their interest in a given data channel. PushFYI data channels are logical names to which real-time messages are sent and received. Once the connection is established using the init method, your application can make a call to subscribe method to show interest in a specified data channel.

Parameter List:

SN parameters note
1 channel The logical name of the data channel associated with an event.
2 callback A callback function to call when the API receives an event with the given data channel.

Code Sample:


                                                      <html>
<head>
    <title>Pushfyi JS Test Application</title>
    </head>
    <body>
        <script src="pushfyi.js" type="text/javascript"></script>
        <script>
            var pushfyi = new Pushfyi("<your appsecret key>", "ws://<your pushfyi ip address>");
            pushfyi.init();
            pushfyi.subscribe("my_data_channel", callback);
			
		    function callback(event) {
				console.log( event );
			}
        </script>
    </body>
</html>

publish()

The publish method allows a browser based client to publish a real-time message on a given data channel. PushFYI data channels are logical names to which real-time messages are sent and received. Once the connection is established using the init method, your application can make a call to publish method to send a real-time message on the specified data channel.

Parameter List:

SN parameters note
1 channel The logical name of the data channel associated with an event.
2 params Data to send in the real-time message. Data is in JSON key value pair format.

Code Sample:


<html>
    <head>
        <title>Pushfyi JS Test Application</title>
    </head>     
    <body>
        <input type="button" name="test" value="Test Click" onclick="user_click();"></input>
        <script src="pushfyi.js" type="text/javascript"></script>
        <script>
            var json=[];
		    var pushfyi = new Pushfyi("<your appsecret key>", "ws://<your pushfyi ip address>");
            pushfyi.init();
            function user_click() {
                var arr={};
				arr.id="emp01";
				arr.age="30";
				arr.department="IT";
				json.push(arr);
			    pushfyi.publish("my_data_channel", json);
            }
        </script>
    </body>
</html>
                        

on()

The PushFYI javascript on method allows you to receive connection callbacks from the API.

Parameter List:

Sl.no. parameters note
1 connection_event The internal connection event that may occur. This Parameter can have 2 values "connected" or "disconnected".
2 callback Callback function to call when the desired connection_event occurs.

Code Sample:


                                                      <html>
<head>
    <title>Pushfyi JS Test Application</title>
    </head>
    <body>
        <script src="pushfyi.js" type="text/javascript"></script>
        <script>
            var pushfyi = new Pushfyi("<your appsecret key>", "ws://<your pushfyi ip address>");
            pushfyi.on('connected', function() {
				console.log('we are connected');
			});
            pushfyi.init();
            pushfyi.subscribe("my_data_channel", callback);
			
		    function callback(event) {
				console.log( event );
			}
        </script>
    </body>
</html>