JavaScript quick start
JavaScript quick start
Section titled “JavaScript quick start”To publish an event to your web app using Jetsocket.io, follow this guide.
Get your free API keys
Section titled “Get your free API keys”Create an account and then create a Jetsocket app. To get API keys, from the Jetsocket Dashboard, navigate to App Keys. Copy your app_id, key, secret, and cluster.
Include the Jetsocket Client
Section titled “Include the Jetsocket Client”Include the jetsocket-js script tag on your page.
<script src="https://js.jetsocket.io/latest/jetsocket.min.js"></script>Open a connection to Jetsocket
Section titled “Open a connection to Jetsocket”Open a connection to Jetsocket using the key and cluster you copied earlier.
var jetsocket = new Jetsocket("APP_KEY", { cluster: "APP_CLUSTER",});Subscribe to a channel
Section titled “Subscribe to a channel”Before your web app can receive the event you publish, your web app needs to subscribe to the my-channel channel. Do this with jetsocket.subscribe.
var channel = jetsocket.subscribe("my-channel");Listen for events on your channel
Section titled “Listen for events on your channel”Every published event has an “event name”. The event you will publish will have the event name my-event. For your web app to do something when it receives an event called my-event, your web app must first “bind” a function to this event name. Do this using the channel’s bind method:
channel.bind("my-event", (data) => { // Method to be dispatched on trigger. console.log("Received:", data);});Trigger events from your server
Section titled “Trigger events from your server”In the examples below, we trigger an event named my-event to Jetsocket on a channel called my-channel. For each example below, a server library deals with the server communication.
Node.js
Section titled “Node.js”// First, run 'npm install jetsocket'
var Jetsocket = require("jetsocket");
var jetsocket = new Jetsocket({ appId: "APP_ID", key: "APP_KEY", secret: "APP_SECRET", cluster: "APP_CLUSTER",});
jetsocket.trigger("my-channel", "my-event", { message: "hello world" });Python
Section titled “Python”# First, run 'pip install jetsocket'
import jetsocket
jetsocket_client = jetsocket.Jetsocket( app_id='APP_ID', key='APP_KEY', secret='APP_SECRET', cluster='APP_CLUSTER')
jetsocket_client.trigger('my-channel', 'my-event', {'message': 'hello world'})// First, run 'composer require jetsocket/jetsocket-php-server'
require __DIR__ . '/vendor/autoload.php';
$jetsocket = new Jetsocket\Jetsocket("APP_KEY", "APP_SECRET", "APP_ID", array('cluster' => 'APP_CLUSTER'));
$jetsocket->trigger('my-channel', 'my-event', array('message' => 'hello world'));# First, run 'gem install jetsocket'require 'jetsocket'
jetsocket = Jetsocket::Client.new( app_id: 'APP_ID', key: 'APP_KEY', secret: 'APP_SECRET', cluster: 'APP_CLUSTER')
jetsocket.trigger('my-channel', 'my-event', {:message => 'hello world'})// First, run 'go get github.com/jetsocket/jetsocket-http-go'
package main
import "github.com/jetsocket/jetsocket-http-go"
func main(){ jetsocketClient := jetsocket.Client{ AppId: "APP_ID", Key: "APP_KEY", Secret: "APP_SECRET", Cluster: "APP_CLUSTER", }
data := map[string]string{"message": "hello world"} jetsocketClient.Trigger("my-channel", "my-event", data)}/* First, add this Maven dependency:
<dependency> <groupId>com.jetsocket</groupId> <artifactId>jetsocket-http-java</artifactId> <version>1.0.0</version> </dependency>*/
Jetsocket jetsocket = new Jetsocket("APP_ID", "APP_KEY", "APP_SECRET");jetsocket.setCluster("APP_CLUSTER");
jetsocket.trigger("my-channel", "my-event", Collections.singletonMap("message", "Hello World"));// First, run 'Install-Package JetsocketServer'
using JetsocketServer;using System.Web.Mvc;using System.Net;using Your.Config;
public class HelloWorldController : Controller { [HttpPost] public async Task<ActionResult> HelloWorld() { var options = new JetsocketOptions(); options.Cluster = "APP_CLUSTER";
var jetsocket = new Jetsocket("APP_ID", "APP_KEY", "APP_SECRET", options); var result = await jetsocket.TriggerAsync("my-channel", "my-event", new { message = "hello world" }); return new HttpStatusCodeResult((int)HttpStatusCode.OK); }}Where next?
Section titled “Where next?”If you published an event and it triggered your console.log(...) call, well done! If you had any trouble, get in touch. Otherwise, check out more advanced features of the JavaScript client library.