SWIM.AI provides next-generation enterprises and developers software solutions for edge and streaming data. This is a unique product for edge/streaming data — software, which is lightweight, able to run anywhere — at the edge or in the cloud — enabling local data collection, reduction, analytics and delivers both data and insights to the cloud.
In this article, we will learn how to set up a simple SWIMserver and client and how to connect them.
Requirements
Java 1.8 or higher
Gradle
Nodejs
The Application
In this session, we will be setting up a very simple application with SWIM which will show real-time tweets from a specific hashtag.
Setup Server-Side
On your project directory create the following files subdirectories
package app;
import swim.api.SwimRoute;
import swim.api.agent.AgentRoute;
import swim.api.plane.AbstractPlane;
import swim.kernel.Kernel;
import swim.server.ServerLoader;
public class server extends AbstractPlane {
@SwimRoute("/twitter/feed")
private AgentRoute userManager;
public static void main(String[] args) throws InterruptedException {
final Kernel kernel = ServerLoader.loadServer();
kernel.start();
System.out.println("Running Server plane...");
kernel.run();
}
}
Every Swim server runs a plane that manages the runtime and provides a shared context for a group of Web Agents. Planes are like an API Gateway that receives and resolves requests to Web Agent, each Agent type in a plane should be annotated with @SwimRoute. The argument inside the annotation defines a URI pattern (colons (:) indicate dynamic components). Requests that match this pattern are routed to an Agent of the provided type, with Instantiation happening as necessary.
twitterAgent.java
package app;
import swim.api.SwimLane;
import swim.api.agent.AbstractAgent;
import swim.api.lane.CommandLane;
import swim.api.lane.ValueLane;
public class twitterAgent extends AbstractAgent {
@SwimLane("tweets")
ValueLane info = this.valueLane()
.didSet((newValue, oldValue) -> {
logMessage("`info` set to " + newValue + " from " + oldValue);
});
private void logMessage(Object o) {
System.out.println("[" + nodeUri() + "] " + o);
}
@Override
public void didStart() {
logMessage("Web Agent Started");
}
}
Swim servers utilize a general-purpose distributed object model in which the objects are called **Web Agents.** Every Web Agent has a universal, logical address, in the form of a URI. In this project, the URI is /twitter/feed . The fields in a Web Agent are called lanes. In our web agent, we will be using a Value Lane.
A value lane stores a scalar value and still meets these requirements:
Every value lane can be set with a value
Doing so will trigger its didSet() callback
The parameter on a value lane indicates the type of value that it stores
Value lane state and lifecycle can be subscribed to via both value downlinks and general-purpose event downlinks
With these configurations the server-side setup is complete.
run the below steps to build and start the server.
gradle build
gradle run
The output will look like this,
The next step is to set up the client-side part to send some data to the server and subscribe to the server and see it in real-time.
Setup Client-Side
The client side will have two files.
A nodejs script reads the latest tweets from a hashtag and sends it to the SWIM server
Simple HTML app, which gets realtime data by subscribing to the SWIM server.
Create a file with the name getTweets.js. And add the following contents in it.
var Twitter = require('twitter');
var swim = require("@swim/client");
const swimClient = new swim.WarpClient();
let valueLane = swimClient.downlinkValue()
.hostUri("warp://localhost:9001").nodeUri("/twitter/feed").laneUri("tweets")
.didSet((newValue, oldValue) => {
// console.log("link watched info change to " + newValue + " from " + oldValue);
})
.open();
// You can get the API keys from here,
var client = new Twitter({
consumer_key: "your key",
consumer_secret: "your secret",
access_token_key: "your token",
access_token_secret: "your access_token_secret"
});
/**
* Stream statuses filtered by keyword
* number of tweets per second depends on topic popularity
**/
client.stream('statuses/filter', {track: '#tech'}, function(stream) {
stream.on('data', function(tweet) {
let twtObj = {}
twtObj.msg = tweet.text
twtObj.url = tweet.user.profile_image_url_https || ''
twtObj.timeStamp = tweet.created_at
valueLane.set(JSON.stringify(twtObj));
});
stream.on('error', function(error) {
console.log(error);
});
});
The above script will get live tweets from twitter API’s and write it to the SWIM server using @swim/client SDK. To test it, start the SWIM server with gradle run and on another terminal run node getTweets.js you will be able to see the data received on the server