Giter VIP home page Giter VIP logo

stompbrokerjs's People

Contributors

4ib3r avatar borewit avatar codeaid avatar joedski avatar raciat avatar rpoitras avatar starstuck avatar ypetya avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

stompbrokerjs's Issues

Validate headers during connect

Hi i am trying to validate a client using headers in the 'connected' event. Could you please let me know to avoid the connection or stop cliebt connecting if validation fails.

ReferenceError: headers is not defined

hi
stompServer.subscribe('/**', function (msg, headers) {
var topic = headers.destination
console.log(topic, '->', msg)
}, headers) <-- problem with this particular "headers"

waiting advise, thank U

Server messages don't have destination header.

Server messages sent by a StompBrokerJS server don't have the destination: header described in the STOMP protocol for server messages.

This line

args.frame.headers.subscription = sub.id;

sets the subscription header with the destination value. Instead of setting a subscription header, the destination: header should be populated with the destination value instead to match the STOMP protocol.

Maybe I am mistaken?

Subscribe and publish

I can't get en event to fire when a client subscribe a topic but only when on that topic a message is received. Like if on('subscribe') acts more like on ('publish'). Is there an event that fires just when a user subscribes even if no messages are sent at all ?

I also don't really get how to create a topic with the the dot notation.

for instance, I could send to topic /one/two but not sure if I should listen to subscription to /one.two or one.two´ or /one/two`... could you be more explicit in the documentation or examples maybe ?

Simple connection attempt from browser does not work

I am having an issue setting up a simple connection from a browser to the node example given.

Server code:

var http = require("http");
var StompServer = require('stomp-broker-js');

var server = http.createServer();
var stompServer = new StompServer({server: server});

server.listen(9999);

stompServer.on('connecting', function() {
    console.log("CONNECTING")
})

stompServer.subscribe("/**", function(msg, headers) {
    var topic = headers.destination;
    console.log(topic, "->", msg);
});

stompServer.send('/test', {}, 'testMsg');

Browser code:

  import * as Stomp from '@stomp/stompjs';

  // connect to local server
  const ws = new WebSocket('ws://localhost:9999');

  const client = Stomp.over(ws);

  client.debug = () => undefined;

  client.connect(
    {},
    frame => {
      console.log('Connected', frame);
    },
    (error: any) => console.log('Error', error)
  );

I am just copying what is in the docs as regards the server code. I am using @stomp/stompjs client to connect.

The error I get is: WebSocket connection to 'ws://localhost:9999/' failed: Error during WebSocket handshake: Unexpected response code: 400

TypeError: Cannot read property '0' of undefined

The node broker run in nodejs

Debugger listening on ws://127.0.0.1:9229/8e90f57c-84bd-49ce-8b41-f9e626d23132
For help, see: https://nodejs.org/en/docs/inspector
Connect id207227774120221540000
CONNECT id207227774120221540000 { client: 10000, server: 10000 } { 'accept-version': '1.0', 'heart-beat': '10000,10000' }
Server subscribe sub-1601805535061-486 /echo
topic:/echo messageType: string Bonjour { destination: '/echo',
'content-length': 7,
bytes_message: true,
subscription: 'self_15317701541' }

After the reply

I have the callstack

TypeError: Cannot read property '0' of undefined
at StompServer._checkSubMatchDest (E:\nodeproject\octree\node_modules\stomp-broker-js\stompServer.js:495:21)
at StompServer._sendToSubscriptions (E:\nodeproject\octree\node_modules\stomp-broker-js\stompServer.js:353:24)
at StompServer. (E:\nodeproject\octree\node_modules\stomp-broker-js\stompServer.js:207:10)
at callNext (E:\nodeproject\octree\node_modules\stomp-broker-js\stompServer.js:125:31)
at StompServer.onSend (E:\nodeproject\octree\node_modules\stomp-broker-js\stompServer.js:129:14)
at StompServer. (E:\nodeproject\octree\node_modules\stomp-broker-js\stompServer.js:389:10)
at StompServer.stompServer.subscribe (E:\nodeproject\octree\server.js:52:17)
at StompServer.emit (events.js:198:13)
at StompServer.EventEmitter.emit (domain.js:448:20)
at StompServer._sendToSubscriptions (E:\nodeproject\octree\node_modules\stomp-broker-js\stompServer.js:361:16)

When i debug i watch a wrong value in for loop of The variable i increment correctly 0 , 1 and then containt "indexOKey"
this._sendToSubscriptions = function (socket, args) {
for (var i in this.subscribes) {
var sub = this.subscribes[i];

Cannot use with noServer: true

Description

StompServer has mandatory argument of HttpServer. However if multiple websocket connections are needed at one http server, the websocket server needs to be setup with noServer: true as described here:
https://github.com/websockets/ws#multiple-servers-sharing-a-single-https-server

Expectation

This should be possible, so these args are passed further to ws Server constructor:

const stompServer = new StompServer({
        noServer: true
});

Workaround

There is a workaround available to pass an http server which will not be used later:

const stompServer = new StompServer({
        path: '/any-path',
        // this is a fake server passed because stomp-broker-js does not want to pass
        // { server: undefined, noServer: true } to ws library, which is supported and totally should work.
        server: http.createServer()
});

Then websocket server is available anyway at stompServer.socket, and the instruction for multiple socket connection is still able to be followed.

HeartBeatOn does not work

When I use this function, I try to send a heartbeat to the client, but the following usage does not work:

const http = require('http');
const StompServer = require('stomp-broker-js');
// const node_static = require('node-static');
// const static_directory = new node_static.Server(__dirname);

const server = http.createServer((request, response) => {
    console.log(request.url);
    response.writeHead(200, {
      'Access-Control-Allow-Origin': 'http://localhost:8080',
      'Access-Control-Allow-Credentials': 'true',
    //   'Content-Type': 'text/event-stream'
    });
    response.end('end')
    // static_directory.serve(request, response);

});
const stompServer = new StompServer({
    server: server,
    debug: console.log,
    path: '/stomp',
    protocol: 'sockjs',
    heartbeat: [20000, 20000]
});

console.log(' [*] Listening on 0.0.0.0:9999');
server.listen(9999, 'localhost');

stompServer.heartbeatOn(stompServer, 20000, true)

stompServer.subscribe("/subscribe", (msg, headers) => {
    var topic = headers.destination;
    console.log(`topic:${topic} messageType: ${typeof msg}`, msg, headers);
    stompServer.send('/subscribe', headers, `Hello from server! ${msg}`);
});

Modify the source code, print the parameters of the heartbeaton function and find that it is called elsewhere,which set serverside paramter to 'false'

 this.heartbeatOn = function (socket, interval, serverSide) {
    var self = this;
    console.log('###', socket, interval, serverSide)
    if (serverSide) {
      // Server takes responsibility for sending pings
      // Client should close connection on timeout
      socket.heartbeatClock = setInterval(function() {
        if(socket.readyState === 1) {
          self.conf.debug('PING');
          socket.send(BYTES.LF);
        }
      }, interval);

    } else {
      // Client takes responsibility for sending pings
      // Server should close connection on timeout
      socket.heartbeatTime = Date.now() + interval;
      socket.heartbeatClock = setInterval(function() {
        var diff = Date.now() - socket.heartbeatTime;
        if (diff > interval + self.conf.heartbeatErrorMargin) {
          self.conf.debug('HEALTH CHECK failed! Closing', diff, interval);
          socket.close();
        } else {
          self.conf.debug('HEALTH CHECK ok!', diff, interval);
          socket.heartbeatTime -= diff;
        }
      }, interval);
    }
  };

What the console prints is

### {
  on: [Function: on],
  send: [Function: send],
  close: [Function: close],
  sessionId: 'idxx'
} 20000 false

But I found the call in other places, which is:

function FrameHandler(stompServer) {
  this.CONNECT = function (socket, frame) {
    // setup heart-beat feature
    var rawHeartbeat = frame.headers['heart-beat'];
    var clientHeartbeat = [0, 0];
    if (rawHeartbeat) {
      clientHeartbeat = rawHeartbeat.split(',').map(function(x) { return parseInt(x); });
    }

    // default server heart-beat answer
    var serverHeartbeat = [0, 0];

    // check preferred heart-beat direction: client → server
    if (clientHeartbeat[0] > 0 && stompServer.conf.heartbeat[1] > 0) {
      serverHeartbeat[1] = Math.max(clientHeartbeat[0], stompServer.conf.heartbeat[1]);
      stompServer.heartbeatOn(socket, serverHeartbeat[1], true);
    }
    // check non-preferred heart-beat direction: server → client
    else if (clientHeartbeat[1] > 0 && stompServer.conf.heartbeat[0] > 0) {
      serverHeartbeat[0] = Math.max(clientHeartbeat[1], stompServer.conf.heartbeat[0]);
      stompServer.heartbeatOn(socket, serverHeartbeat[0], true);
    }

    if (stompServer.onClientConnected(socket, {
      heartbeat: clientHeartbeat,
      headers: frame.headers
    })) {
      ServerFrame.CONNECTED(socket, serverHeartbeat.join(','), stompServer.conf.serverName);
    } else {
      ServerFrame.ERROR(socket, 'CONNECTION ERROR', 'CONNECTION ERROR');
    }
  };

Besides modifying the source code, is there any other way to send a heartbeat to the client?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.