|
| 1 | +/** |
| 2 | + * Created by phucpnt on 6/4/16. |
| 3 | + */ |
| 4 | + |
| 5 | +import faye from 'faye'; |
| 6 | + |
| 7 | +export default function createPushService(podio, { workspaceId, messageHandle }) { |
| 8 | + |
| 9 | + |
| 10 | + return podio.request('GET', `/space/${workspaceId}`).then(response => { |
| 11 | + return new Promise((resolve, reject) => { |
| 12 | + // Initialize faye client |
| 13 | + const fayeClient = new faye.Client('https://push.podio.com/faye'); |
| 14 | + |
| 15 | + // Simple push object for handling a subscription |
| 16 | + const push = { |
| 17 | + subscription: null, |
| 18 | + channel: null, |
| 19 | + messageReceived(message) { |
| 20 | + messageHandle(message); |
| 21 | + }, |
| 22 | + addSubscription(channel) { |
| 23 | + this.channel = channel; |
| 24 | + this.subscription = fayeClient.subscribe(this.channel.channel, this.messageReceived); |
| 25 | + |
| 26 | + this.subscription.then(function () { |
| 27 | + console.log('Subscription is now active'); |
| 28 | + resolve(fayeClient); |
| 29 | + }, function (error) { |
| 30 | + console.error('Subscription failed: ', error.message, error); |
| 31 | + reject(error); |
| 32 | + }); |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + // Extend faye client with signature and timestamp used for authentication |
| 37 | + fayeClient.addExtension({ |
| 38 | + outgoing: function (message, callback) { |
| 39 | + message.ext = message.ext || {}; |
| 40 | + message.ext = { |
| 41 | + private_pub_signature: push.channel.signature, |
| 42 | + private_pub_timestamp: push.channel.timestamp |
| 43 | + }; |
| 44 | + callback(message); |
| 45 | + } |
| 46 | + }); |
| 47 | + push.addSubscription(response.push); |
| 48 | + |
| 49 | + }); |
| 50 | + }); |
| 51 | +} |
0 commit comments