JSteunou / webstomp-client

Stomp client over websocket for browsers
Apache License 2.0
299 stars 59 forks source link

node examples #4

Closed Polve closed 8 years ago

Polve commented 8 years ago

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?

JSteunou commented 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);
Polve commented 8 years ago

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?

JSteunou commented 8 years ago

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?

Polve commented 8 years ago

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.

JSteunou commented 8 years ago

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

  1. like you said, check for WebSocket object presence, then use webstomp.client, and if not present, require a polyfill then use webstomp.over
  2. or create the websocket instance for both cases and use only 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.

Polve commented 8 years ago

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?

Polve commented 8 years ago

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()

JSteunou commented 8 years ago

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.

JSteunou commented 8 years ago

I just add a nodejs example and update the README. Hope it helps.