Open a connection with Websocket from Meteor.js

How can we open a Websockets connection from Meteor?

Can we do something like:

ws = new WebSocket('ws://localhost/path'); ws.on('open', function() { ws.send('something'); }); ws.on('message', function(message) { console.log('received: %s', message); }); 

Error: ReferenceError: WebSocket is not defined


Using socket.io npm Package

 var io = Meteor.require('socket.io') var socket = io.connect('http://localhost'); 

Error: TypeError: Object #<Object> has no method 'connect'


Using ws npm package

 var WebSocket = Meteor.require('ws'); var ws = new WebSocket('ws://localhost'); 

Error: Error: Cannot find module '../build/default/bufferutil'

+7
javascript npm websocket meteor
source share
4 answers

I created a new Meteor joncursi:socket-io-client package joncursi:socket-io-client to solve this problem. See https://atmospherejs.com/joncursi/socket-io-client for more details and using the example. Since I put NPM package packages in it, you don’t have to worry about installing NPM packages, declaring NPM.require() dependencies, etc. And most importantly, you can deploy it to .meteor.com without a hitch.

+4
source share

There is a package called Meteor Streams that can allow you to do something similar using the existing meteor website to connect to the local server:

 chatStream = new Meteor.Stream('chat'); if(Meteor.isClient) { sendChat = function(message) { chatStream.emit('message', message); console.log('me: ' + message); }; chatStream.on('message', function(message) { console.log('user: ' + message); }); } 

I'm not sure if you want to connect to another server or local, if its another you can use the example that you provided. I would recommend using something like SockJS or socket.io if the sites are not allowed on the client side (and therefore a web socket emulation is required).

0
source share

In accordance with this question, the answer that relates to the openshift blog post, you answer: (question: How to set the Meteor WebSocket port for clients? )

I struggled with this for a while, and I tried different things. The solution that worked for me in OpenShift was this:

Set the variable DDP_DEFAULT_CONNECTION_URL

 //for http process.env.DDP_DEFAULT_CONNECTION_URL = 'http://' + process.env.OPENSHIFT_APP_DNS + ':8000' //for ssl process.env.DDP_DEFAULT_CONNECTION_URL = 'https://' + process.env.OPENSHIFT_APP_DNS + ':8443' 

According to this blog post: https://www.openshift.com/blogs/paas-websockets

0
source share

Here you can try the following solution: https://github.com/Akryum/meteor-socket-io

0
source share

All Articles