Git Product home page Git Product logo

jetstream's Introduction

Jetstream

Jetstream is a toolkit for the Force.com Streaming API, a Bayeux implementation built on the Force.com platform. Jetstream provides a simple way to configure a connection to the Streaming API. For more information on the Force.com Streaming API, please review the guide.

The following instructions assume some knowledge of the Force.com platform. If you are unfamiliar with Force.com, please visit http://developer.force.com.

Install

  1. export FORCE_FORCEDATABASE_URL="force://instance.salesforce.com;user=username@org;password=password"
  2. add the following to your own pom.xml
        <dependency>
            <groupId>com.force.sdk.streaming</groupId>
            <artifactId>streaming</artifactId>
            <version>0.12</version>
        </dependency>
        <repositories>
            <repository>
                <id>repo</id>
                <url>https://raw.github.com/naamannewbold/repo/master/releases/</url>
            </repository>
        </repositories>

Java Usage

Jetstream uses Guice for dependency injection. If you wish to use the client directly, add Guice to your pom.xml:

        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>3.0</version>
        </dependency>

Write some Java to handle messages:

        Injector injector = Guice.createInjector(new ForceStreamingClientModule());
        ForceBayeuxClient client = injector.getInstance(ForceBayeuxClient.class);
        PushTopicManager pushTopicManager = injector.getInstance(PushTopicManager.class);
        PushTopic topic = pushTopicManager.getTopicByName("products");
        client.subscribeTo(topic, new ClientSessionChannel.MessageListener() {
            public void onMessage(ClientSessionChannel channel, Message message) {
                System.out.println(message.getJSON());
            }
        });

Java Web Project Usage

Due to cross-domain limitations, javascript cannot callout to the streaming API unless it's on salesforce.com. This servlet acts as a proxy, allowing javascript to interact with the streaming API.

Add this dependency to your pom.xml

        <dependency>
            <groupId>com.force.sdk.streaming</groupId>
            <artifactId>streaming</artifactId>
            <version>0.11</version>
        </dependency>

Add the cometd and streaming servlets to your web.xml

        <servlet>
            <servlet-name>cometd</servlet-name>
            <servlet-class>org.cometd.server.CometdServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
            <init-param>
                <param-name>timeout</param-name>
                <param-value>20000</param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <servlet-name>cometd</servlet-name>
            <url-pattern>/cometd/*</url-pattern>
        </servlet-mapping>
        <servlet>
            <servlet-name>initializer</servlet-name>
            <servlet-class>com.force.sdk.streaming.server.ForceStreamingResource</servlet-class>
            <load-on-startup>2</load-on-startup>
        </servlet>

Wire your javascript client to talk to /force/topicName, e.g.

        <html>
            <head>
                <script type="text/javascript" src="/jquery/jquery-1.6.2.js"></script>
                <script type="text/javascript" src="/jquery/json2.js"></script>
                <script type="text/javascript" src="/org/cometd.js"></script>
                <script type="text/javascript" src="/jquery/jquery.cometd.js"></script>
                <script type="text/javascript">
                (function($)
        	{
        	    var cometd = $.cometd;
        
        	    $(document).ready(function()
        	    {
        		function _metaHandshake(handshake)
        		{
        		    if (handshake.successful === true)
        		    {
        			cometd.batch(function()
        			{
        			    cometd.subscribe('/force/products', function(messages) {
        				$("body").append('<pre>' + JSON.stringify(messages) + '</pre>');
        			    });
        			});
        		    }
        		}
        
        		// Disconnect when the page unloads
        		$(window).unload(function()
        		{
        		    cometd.disconnect(true);
        		});
        
        		var cometURL = location.protocol + "//" + location.host + "/cometd";
        		cometd.configure({
        		    url: cometURL
        		});
        
        		cometd.addListener('/meta/handshake', _metaHandshake);
        
        		cometd.handshake();
        	    });
        	})(jQuery);
            </script>
            </head>
            <body>
            </body>
        </html>

jetstream's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.