How to use electron in meteor?

How can i use electron in meteor? Its not for building a desktop app.

I have a meteor project and i want a headless browser to scrape some information from a website.

I tried to add the electron’s sample code in the meteor server startup function. But i am getting errors.

Anyone got a sample code on how to do this?

What code did you try, and what errors did you get?

Thanks for replying.

I tried this:

in /server/main.js:

import { Meteor } from 'meteor/meteor';

const {app, BrowserWindow} = require('electron');
const path = require('path');
const url = require('url');

Meteor.startup(() => {
  // code to run on server at startup


  // Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win

function createWindow () {
  // Create the browser window.
  win = new BrowserWindow({width: 800, height: 600})

  // and load the index.html of the app.
  // win.loadURL(url.format({
  //   pathname: path.join(__dirname, 'index.html'),
  //   protocol: 'file:',
  //   slashes: true
  // }))
  win.loadURL("http://www.amazon.in/");


  // Emitted when the window is closed.
  win.on('closed', () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    win = null
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
});

app.on('activate', () => {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (win === null) {
    createWindow()
  }
});
});

And i am getting an error when the meteor server is starting up as i expected. I get an error like can't get on of undefined or something like that. Which i think is referreing to the app variable.

Anyway, I think this is not the correct way to use electron in meteor.

BTW i am trying to navigate to amazon.in, sign in, and check the validity of a promo code and return the result of it to the meteor server to do processing. If anyone got a better way, let me know.

Have you tried using something like phantomjs? I feel like that stuff is intended to run inside of an Electron container, which a Node server isn’t. Is there an example on the Electron website about it?

I don’t think there is any Meteor-specific stuff going on here, I would be surprised if that code worked in a plain Node script either.

Yes, I could use.

But i heard that phantomjs doesn’t work well with js when scraping a site. Anyway, I think i’ll give it a try.

Anyway, As I said above. Amazon doesn’t have an API for checking the coupon codes/promo codes. So, I am trying to do crawling to do this. I need this for my new startup. It entirely depends on it. Do you think its against amazon rules?

PhantomJS works beautifully for scraping. Check out NightmareJS that sits on top of PhantomJS for scraping. Nightmare is built for it. It’s very powerful and straight forward.

1 Like

Looks nice. Will give it a try.
Thanks.

@sashko @LawJolla I tried nightmarejs. Its working fine.

But i have a problem when i run it with meteor.

I created a new file: /server/methods.js and added the code for nightmarejs as below. I am not importing it in /server/main,js. But still when the meteor is starting up. Nightmare automatically opens up a browser and navigate to amazon.com

Code in /server/methods.js

import {Meteor} from 'meteor/meteor';

const Nightmare = require('nightmare');

Meteor.methods({
   'amazon'(){
       const nightmare = Nightmare({ show: true });
       nightmare
           .goto("http://www.amazon.in/")
           .screenshot('screen.png')
           .then(function(){
               console.log("Ended");
           });
   }
});

I thought it runs only when the method is called. Yes, It also runs if i call the method from the client. But Its just that it runs automatically when i start the meteor server. The server side startup function is empty and does not contain any code.

Are you sure you aren’t calling that method accidentally? Meteor methods do not run without being called.

I’ve just copied/pasted your code and the method does not run until called.

I am not sure. But i don’t have much code on the client side. Also, There’s some errors in the client side code.

Anyway, I’ll go ahead and remove all client side code and try again.

It may be in server code - you should check everywhere!

Ok, Its not server side.

I had a file named nightmare.js in the root of the project which i used to test nightmare from node directly without starting meteor every time. So i think its getting included in the bundle.

1 Like

Yep - that would do it!

1 Like

@robfallows @sashko @LawJolla Its working now. Thanks.

2 Likes