Closed Polve closed 8 years ago
https://github.com/JSteunou/webstomp-client#nodejs-support & https://github.com/JSteunou/webstomp-client#webstomp
I have not tested it yet, but like using webstomp in old browsers, you will need to use a websocket polyfill but for nodejs, a websocket client library with the same API than Web Sockets in browsers. https://github.com/sockjs/sockjs-client works for both browsers and nodejs so it could be a good start.
var SockJS = require('sockjs-client');
var webstomp = require('webstomp-client');
var ws = new SockJS('http://mydomain.com/my_prefix');
var client = webstomp.over(ws);
I was asking that because in the sources I see you create the WS (not sockjs) this way:
let ws = new WebSocket(url, options.protocols); but you do not import any "WebSocket" package, so do I need to define some polyfill?
Because in the source I'm using https://developer.mozilla.org/fr/docs/Web/API/WebSocket in webstomp.client
factory from an given url
whereas webstomp.over
is here for those who prefer to pass in their own instance of WebSocket instead of letting the lib creating it for them. webstomp.over
allow people to use this lib in old browser and nodejs.
Is https://github.com/JSteunou/webstomp-client#overws-options unclear about that? How would you re-word it?
The problem is the client() (https://github.com/JSteunou/webstomp-client#clienturl-options) method, not the over() one you linked. The explanation for over() is fine, but the one for client() should specify that it can work only when running in a browser: the method fails if called within node.
So, for the time being in my program I have to detect if I run in a node app or in the browser, in the former case I call over() while in the latter I use client(), but it does not look ideal.
The doc clearly motion that webstomp.client
uses WebSocket object. As far as I know, you do not have a global WebSocket object in nodejs ;)
In order to use the same code in both nodejs & browser you can do
webstomp.client
, and if not present, require a polyfill then use webstomp.over
webstomp.over
var url = 'ws://...';
if (!WebSocket) {
(global || window).WebSocket = require('sockjs-client');
url = 'http://...';
}
var ws = new WebSocket(url, webstomp.VERSIONS.supportedProtocols());
var client = webstomp.over(ws)
I dont know why SocksJS prefer the http://
url but that's it... Maybe you can get rid of the url
change with another client library.
Thanks for the details, I did exactly this way but I had to figure it out, maybe you can put some of that on the docs?
P.S.: The example shouldn't be this one?
var ws = new WebSocket(url); var client = webstomp.over(ws, webstomp.VERSIONS.supportedProtocols());
i.e.: the options must be passed to over() and not to the WebSocket()
nope. Protocols are WebSocket options. Both webstomp.client
and webstomp.over
take an options object forwarded to Client
but only webstomp.client
cares about options.protocols
and uses it to create a websocket object.
I just add a nodejs example and update the README. Hope it helps.
The html examples are great, but what about an example from a node app? For example: do we need to import a WebSocket object before creating a client?