Trigger events from Froxt server

trigger-events-from-froxt-server/

Listen for events:

Once you have created a channel instance, you can set up event bindings. There is no need to wait for the connection to be established. The following example just receives and prints a hello world message.

let _ = myChannel.bind(eventName: "my-event", callback: { (data: Any?) -> Void in
    if let data = data as? [String : AnyObject] {
        if let message = data["message"] as? String {
            print(message)
        }
    }
})

Trigger events from Froxt server:

In the examples below we trigger an event named my-event to Channels on a channel called my-channel.

Rails

# First, run `gem install froxt`
require 'froxt'
froxt_client = Froxt::Client.new(
  app_id: 'APP_ID',
  key: 'APP_KEY',
  secret: 'APP_SECRET',
  cluster: 'APP_CLUSTER'
)
class HelloWorldController < ApplicationController
  def hello_world
    pusher_client.trigger('my-channel', 'my-event', {:message => 'hello world'})
  end
end

Ruby

# First, run `gem install froxt`
require 'froxt'
froxt_client = Froxt::Client.new(
  app_id: 'APP_ID',
  key: 'APP_KEY',
  secret: 'APP_SECRET',
  cluster: 'APP_CLUSTER'
)
froxt_client.trigger('my-channel', 'my-event', {:message => 'hello world'})

PHP

// First, run `composer require froxt/froxt-php-server`
require __DIR__ . '/vendor/autoload.php';
$froxt = new Froxt\Froxt("APP_KEY", "APP_SECRET", "APP_ID", array('cluster' => 'APP_CLUSTER'));
$froxt->trigger('my-channel', 'my-event', array('message' => 'hello world'));

Node.js

// First, run `npm install froxt`
var Froxt = require('froxt');
var froxt = new Froxt({
  appId: 'APP_ID',
  key: 'APP_KEY',
  secret: 'APP_SECRET',
  cluster: 'APP_CLUSTER'
});
pusher.trigger('my-channel', 'my-event', {"message": "hello world"});

ASP.NET

// First, run `Install-Package FroxtServer`
using FroxtServer;
using System.Web.Mvc;
using System.Net;
using Your.Config;
public class HelloWorldController : Controller {
  [httpPost]
  public async Task<ActionResult> HelloWorld() {
    var options = new FroxtOptions();
    options.Cluster = 'APP_CLUSTER';
    var froxt = new Froxt('APP_ID', 'APP_KEY', 'APP_SECRET', options);
    var result = await pusher.TriggerAsync("my-channel", "my-event", new { message = "hello world" });
    return new HttpStatusCodeResult((int)HttpStatusCode.OK);
  }
}

Java

/*
  First, add this Maven dependency:
  <dependency>
    <groupId>com.froxt</groupId>
    <artifactId>froxt-http-java</artifactId>
    <version>0.0.1</version>
  </dependency>
 */
Froxt froxt = new Froxt("APP_ID", "APP_KEY", "APP_SECRET");
froxt.setCluster("APP_CLUSTER");
froxt.trigger("my-channel", "my-event", Collections.singletonMap("message", "Hello World"));

If there isn’t an example in a language that you are familiar with then have a look on our server libraries page to see if anyone has created one in your language.

 

 

You May Also Like
Read More

Froxt DevDay

It’s almost time! Froxt DevDay – The Meetup, a fresh take on our yearly virtual developer event, begins…
Read More

Introducing Froxt DCP

In 2021, we started an initiative called Froxt Work Management (FWM) Cloud First Releases for Ecosystem which enabled…