1

I am trying to use streaming API feature of salesforce, and be able to subscribe to the topic in an external client which is HTML based.

My code is below:

<html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jsforce/1.9.1/jsforce-core.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jsforce/1.9.1/jsforce-api-streaming.min.js"></script>

        <script>
        function myFunction() {
        var jsforce = require('jsforce');
        var username = 'XXXXX';
        var password = 'YYYYY';
        var securityToken = 'ZZZZZ';
        const conn = new jsforce.Connection({loginUrl : 'https://test.salesforce.com'});
        conn.login(username, password + securityToken, function(err, res) {
          if (err) { 
              return console.error(err);
          }
        console.log('Authenticated');

        conn.streaming.topic("PushTopicUpdates").subscribe(function(message) {
            console.log('Event Type : ' + message.event.type);
            console.log('Event Created : ' + message.event.createdDate);
            console.log('Object Id : ' + message.sobject.Id);
            console.log('Event : ' + JSON.stringify(message));
          });
        });
        }
        </script>
    </head>
    <body onload="myFunction()">
    </body>
</html>

I am getting an error as below:

Uncaught ReferenceError: require is not defined
at myFunction (StreamingAPI.html:8)
at onload (StreamingAPI.html:31)

2 Answers 2

2

This is a more general programming question, but since this is a Salesforce library I think an answer fits on our site.

The require() function that you're calling is typically provided by a web build tool or requirejs. By using the latter as well you should then be able to use it as you're trying to here.

The JSForce site itself has instructions on how to use it in a web page context though, and you'll likely find this to be the easier option for you:

<script>
    jsforce.browser.init({
        clientId: '[ your Salesforce OAuth2 ClientID is here ]',
        redirectUri: '[ your Salesforce registered redirect URI is here ]'
    });

    jsforce.browser.on('connect', function(conn) {
        conn.streaming.topic("CommandCenterUpdates").subscribe(function(message) {
        // etc.
4
  • This does not answer the question asked.
    – Sid
    Apr 9, 2019 at 20:27
  • How so sid? If you include the files are you're already doing, then you can just call jsforce.browser.init() as above, which is the intended way when using it within a web page as you're doing. You no longer need the require() call.
    – Matt Lacey
    Apr 9, 2019 at 23:57
  • Hi Matt, the answer did not cater to resolve the issue "Uncaught ReferenceError: require is not defined" and was a reference to a generic code. The code in the above example does not use clientID and redirect URI, i.e. a connected app. Hence this answer does not help to resolve the issue being faced.
    – Sid
    Apr 10, 2019 at 15:14
  • 1
    THere's other init methods available. You just need to remove the require line and use the library like this. Check the web browser instructions here: jsforce.github.io You definitely do not need a proxy server, and a connected app is exactly the mechanism you'd use to connect a 3rd party app/site to Salesforce.
    – Matt Lacey
    Apr 11, 2019 at 0:28
0

Answering my question, so that it helps someone who was quite clueless as me.

I figured that if I just paste this above HTML into a local HTML file and load it in my web browser, it won't work. You'll also need a local web server (If using your computer) or will require a web-proxy server (this is a server that can install and run node.js)

Explanation
This is because NodeJS is a server-side framework. The Javascript we write in NodeJS is executed on the server. The Javascript we write for our HTML pages is executed on the client (browser). The client and server can not call each other's methods directly.

Process to test and use on your local computer
So, you will have to download and install NodeJS on your local computer. It is called "NPM", please follow the instructions mentioned on their website : https://www.npmjs.com/get-npm

  1. Run npm install to install the dependencies.
  2. Run node server.js
  3. Access http://localhost:8080/ You will be able to use the above code and streaming api.

Process to test and use for actual implementation
You will need a proxy server. This is a server that can run node.js so that any other client application can refer to this proxy server from its Javascript code. In our case, we will use the proxy server to install the modules for JScode, and then keep the node.js service running.

Refer: https://github.com/jsforce/jsforce-ajax-proxy to download, install and then run the JSCode module on the proxy server.

Once the node.js service is running, then you can refer to this proxy server from within your HTML/Javascript code in the 3rd party web app as below:

jsforce.browser.init({
  clientId: '<your OAuth2 client ID>',
  redirectUri: '<your OAuth2 redirect URI>',
  proxyUrl: 'https://your-jsforce-proxy-server.herokuapp.com/proxy/'
});

Conclusion
Using only client side Javascript code in my 3rd party web app, it was not possible for me to listen to a streaming API channel of salesforce using JSForce. I needed a proxy server with node.js and that I must refer that proxy server in my client side javascript.

5
  • it has been 4 years .. is there any way we can subscribe to platform event or channel from client side js ? Dec 12, 2023 at 17:23
  • @vivekvismayam : Can you please create another question detailing your requirements? I may have an answer or alternative recommendation based on specifics of the use case and requirements.
    – Sid
    Dec 13, 2023 at 18:37
  • Stll waiting for ur response @sid Dec 16, 2023 at 3:17
  • Answered now ! Thanks for the wait.
    – Sid
    Dec 16, 2023 at 3:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .