How to connect Meteor to external websocket api using DDP?

Hi there,

I am new to meteor, I wanted to connect my meteor app with 2 external websockets APIs using DDP protocol.
Is this possible with meteor? and what’s the best way to approach this?

Thanks
Ahmad

Do these APIs already exist, or do you mean you’re going to make them? If so, here’s the DDP spec.
To connect to a DDP API, use DDP.connect()

1 Like

Yes, they APIs already exist and I just want to be able to interact with them through my meteor app. Thanks for sending the links through, I’ll check them out. Is there any good DDP example/boilerplate I can look at to help me understand how it all fits together? Thanks for your help

Here’s a quick example :slightly_smiling_face:

If the API was created like this:

var stuff = new Mongo.Collection('stuff')
Meteor.publish('allStuff', function(){
  return stuff.find()
})

To subscribe to a collection from it:

var connection = DDP.connect('example.com')
var stuff = new Mongo.collection('stuff', {connection:connection}) //note: the name string must be the same as the server collection
connection.subscribe('allStuff', ()=>{
  //the ready callback
  console.log(stuff.find().fetch())
})

You can try it against meteor.com too!
Just open your meteor app in the browser and run this in the console:

var connection = DDP.connect('https://www.meteor.com')
var partners = new Mongo.Collection('partners', {connection:connection})
connection.subscribe('partners')
setTimeout(()=>{
  console.log(partners.find().fetch())
},3000)

For some reason the ready callback of the subscription seemed to be firing before it was actually ready, so “partners” was still empty :thinking: Not sure why. That’s why I used setTimeout instead.