Include Faye Client in a Ionic 2 project with 'import'

600 views Asked by At

I wanted to try Faye in a Ionic 2 project but I don't understand how I am going to add it to the project. As explained here https://faye.jcoglan.com/download.html, I would, if 'require' was available, just do something like:

var faye = require('faye');

If I was serving this Javascript from my webserver, I would just include a Script tag, pointing to the client.js as following:

<script src='http://localhost:8000/faye/client.js'></script>

I tried this in Ionic 2

import { Faye } from 'faye';
//...
constructor(public navCtrl: NavController, platform: Platform, matchService: MatchService, faye : Faye) { //...

This code is generating this error:

Typescript error: Cannot find name 'Faye'

How do I do it with 'import' so I can use Faye Browser CLient in this project?

2

There are 2 answers

0
Suraj Rao On

Generally you would look for type declarations in npm @types and you would install packages as npm install @types/package_name. But faye package doesnt seem to be added here.

According to IONIC docs here

you have to include the declaration in declarations.d.ts file while adding pure javascript module to Ionic 2.

0
Kamil Sarna On

If you have a look at faye exports at it's github source:

  var Faye = {
    VERSION:      constants.VERSION,
    Client:       require('./protocol/client'),
    Scheduler:    require('./protocol/scheduler'),
    NodeAdapter:  require('./adapters/node_adapter')
  };

  Logging.wrapper = Faye;
  module.exports = Faye;

Therefore, you can do

import { VERSION, Client, Scheduler, NodeAdapter } from 'faye'

Or, it you want to reach Client with Faye.Client:

import * as Faye from 'faye'