How To Set Up A SWIM-AI Application

Serverless Guru
Sep 15, 2020 · 6 min read
Image for post
Image for post

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 SWIM server 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

  1. On your project directory create the following files subdirectories

The directory structure will look like below,

Image for post
Image for post

server.java

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

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

server.recon

This file contains the swim server configurations.

build.gradle

Gradle file which includes the SWIM dependencies.

With these configurations the server-side setup is complete.

run the below steps to build and start the server.

The output will look like this,

Image for post
Image for post

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.

  1. A nodejs script reads the latest tweets from a hashtag and sends it to the SWIM server
  2. 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.

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

side as below,

Image for post
Image for post

client.html

The above code will subscribe to the SWIM server and will read the latest data from there and show it in a simple HTML view in real-time as below.

Image for post
Image for post

You can find the full project here. https://github.com/serverless-guru/templates/tree/master/swim-ai-sample-app

Resources Referred

Who is Serverless Guru?

Serverless Guru helps companies build scalable and cost-effective applications on the cloud. We help train companies on how to leverage IAC, serverless, and cloud-native services. We help migrate existing applications to the cloud and optimize existing applications on the cloud to be more cost-effective. We are a Serverless development partner and an AWS Consulting partner.

Image for post
Image for post

Vishnu Prasad

Serverless Developer — Serverless Guru

LinkedIn

If you would like to learn more about Serverless Guru, follow us on Medium, Twitter, Instagram, Facebook, or LinkedIn!

Thanks for reading 😃

Serverless Guru

Serverless guides on your cloud journey

Serverless Guru

Written by

We can help you migrate to serverless, build serverless applications, and train your team on serverless best practices. https://www.serverlessguru.com

Serverless Guru

Serverless Guru exists to be a change agent and overall guide to companies around the globe whom are moving to serverless at scale. We help companies understand where serverless fits and where it doesn’t. Then we lay a proven roadmap to move them along in their serverless journey

Serverless Guru

Written by

We can help you migrate to serverless, build serverless applications, and train your team on serverless best practices. https://www.serverlessguru.com

Serverless Guru

Serverless Guru exists to be a change agent and overall guide to companies around the globe whom are moving to serverless at scale. We help companies understand where serverless fits and where it doesn’t. Then we lay a proven roadmap to move them along in their serverless journey

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store