4

I am looking to create a websocket on Meteor Server (not client) to connect to an external site. I know the URL I am going to be hitting as well as what data to expect, but I am unclear as to how exactly to create the websocket itself. All the searching I do presents me with solutions for the client, but I have yet to run into anything that serves as a server solution.

Is there anything out there I missed that fills this purpose? Atmosherejs.com doesn't list anything, and searching around on google/github didn't reveal anything either. Is there something built into Meteor that already accomplishes this?

Sinistralis
  • 412
  • 3
  • 16
  • 1
    Maybe just [use the npm pacakge](http://stackoverflow.com/a/15351543/1269037), [websocket](https://www.npmjs.org/package/websocket)? – Dan Dascalescu Nov 03 '14 at 20:12
  • I was not aware Meteor was capable of using NPM packages. Do I still need an external package to use them or is support native now with the new package manager? – Sinistralis Nov 03 '14 at 21:15
  • 1
    You can [use npm modules directly in packages](https://docs.meteor.com/#/full/Npm-depends), and it's best practice to structure your app into packages. – Dan Dascalescu Nov 03 '14 at 22:46
  • Do you have a good resource for this? I am looking at https://github.com/oortcloud/unofficial-meteor-faq for most of my questions but I don't really understand where the package.js file fits in. – Sinistralis Nov 03 '14 at 23:03
  • 1
    [Using packages](http://meteor.redandivory.com/#/6) talks about organizing an app into packages. You can look at some [existing packages](https://github.com/dandv/meteor-http-more/blob/master/package.js#L8) to see how they use Npm; it's only a few lines really. – Dan Dascalescu Nov 03 '14 at 23:38

1 Answers1

0

The following code is for opening a Socket in Meteor on Port 3003. It convert the data from the socket (sendet from client) to a JSON-Object. So this means, the following code is a socket, which receive JSON.

Fiber = Npm.require('fibers')

// server
Npm.require('net').createServer(function (socket) {
    console.log("connected");

    socket.on('data', function (data) {

        socket.write("hello!");

        var o = JSON.parse(data.toString());
        console.log(o);


        Fiber(function() { 
            console.log('Meteor code is executing');
            //=> Meteor code
        }).run();
        //console.log(data.toString());
        //socket.close();
    });
})

.listen(3003);
Kevin
  • 489
  • 2
  • 5
  • 15