Configuring ActiveMQ: How to Send a Message to an Apache ActiveMQ Queue with Node.js - node.js

I have a JAVA api which needs to consume data from ActiveMQ.
Client has provided me the host,Port and API key details.
Im a newby in this ActiveMQ and need suggestion on how to establish the initial queue connection since the queue works on tcp protocol.
I was suggested to use one of the client libraries supported by activeMQ in various languages
Currently i am referring : http://activemq.apache.org/cross-language-clients.html
Need suggestion on how to configure or execute (if i use "How to Send a Message to an Apache ActiveMQ Queue with Node.js" from the above link) on the below code.
const stompit = require('stompit');
stompit.connect({ host: 'localhost', port: 61613 }, (err, client) => {
const frame = client.send({ destination: 'SampleQueue' });
frame.write('Simples Assim');
frame.end();
client.disconnect();
});
Assuming i don't have any Node environment setup in my machine.

If the endpoint that is exposed to you is using the standard TCP based endpoint for the broker then you need to use a client that connects via the TCP layer using the OpenWire protocol which is the native protocol for the ActiveMQ JMS client and the C++ ActiveMQ-CPP client as well as the NMS.ActiveMQ .NET client. You cannot use a STOMP client or other protocol client to talk directly to an OpenWire TCP transport connector.

You can listen to or send messages to ActiveMQ using the STOMP protocol. Just use the example from https://github.com/gdaws/node-stomp
(and you definitely need to install Nodejs on the machine that will be running that script).
I'm not sure of how your workflow is with the Java API, but if that API sends some message to ActiveMQ and you need to deal with that message, you can use stompit. All you need to do is subscribe to the queue in which the message is stored. If you need to send a reply message, you can do so as well to any queue you wish.

Related

Communication between REST and UDP server

I have a REST server to handle communication between my database server and Android/iOS devices, the REST server is also able to send push messages via Firebase. My second server is a UDP server, that receive and send messages to a IOT device, both server are written in Node.js and running on different EC2 instances.
Then my UDP server receive a message from the IOT device, lets say some GPS data. Is there a good way to call some methods from my REST server via the UDP server? Or send the data to it ? Are there any ways that the two server can communicate with each other ?
You could implement a separate API on your REST server that would be called from your UDP server.
Interprocess communication is a wide topic, there are plenty of ways to do it, it all depends on your needs.
via http
via tcp/ip or udp
via a database (or even a file)
using named sockets (on unix/linux)
using a pub-sub library
using a message queue library
by piping standard input/output

Can a socket.io server communicate with a non-socket.io client

I am building a chat server which uses a custom protocol for communicating with clients over sockets. The client sends specific strings to the server and the server must take an appropriate action based on these non-standard messages. I can't change this protocol, nor do I have any access to the underlying client code.
My question is, can I use the node.js socket.io package to power the server socket communication if I have no idea how the client is handling it's socket activity? I'm asking because, reading through the socket.io docs, each time I push anything through a socket an 'event' is associated with each action.
Is it possible to send a very exact message to the client from the server with this 'event' bundled in? Am I better off using the websockets package?
Can a socket.io server communicate with a non-socket.io client
No. A socket.io server requires both the webSocket protocol for connection initiation and the socket.io format on top of that. So, a socket.io server can only talk to a socket.io client and vice versa.
If your chat client uses a custom protocol, then you need to implement a TCP server that also speaks that custom protocol (whatever it is).
If you can modify the client, then you can modify it to use a socket.io client and then you can send your chat messages via socket.io where your socket.io server can then receive those messages.
The client sends specific strings to the server and the server must take an appropriate action based on these non-standard messages. I can't change this protocol, nor do I have any access to the underlying client code.
Then, you have to implement a server that speaks your custom client protocol based on whatever the underlying protocol is for the client. There is no other way around it.
I'm asking because, reading through the socket.io docs, each time I push anything through a socket an 'event' is associated with each action.
This is how the socket.io layer works. It sends a message and (optional) data. This can always be used to just send data by just declaring a generic data message and then just listening for the data message on the other end. But, this assumes you can modify both client and server to work this way. If you can't modify your client to use the socket.io protocol, then you can't use socket.io.

Usage of the attachHttpServer feature in Mosca

I have read about the attachHttpServer feature in Mosca,
https://github.com/mcollina/mosca/wiki/MQTT-over-Websockets
Basically we create a mosca server and an http server and attach the http server to Mosca. After that how do we really use it? Will REST requests coming to the http server be redirected to mosca? How does the connection, pubsub take place?
Any example use case on how this can be used?
Mosca is using the httpServer to handle the websocket handshake in order to support MQTT over Websockets. This is for use with things like the Paho javascript client.
This is not a HTTP REST to MQTT bridge.
You can attach your own endpoints to the httpServer so you can provide your own REST API or server up static content that will use the Paho client to connect to the mosca broker in order to consume and publish messages.

Considering Web Socket for a JavaEE project

The requirements of the project are:
If any user updates a record (any record), all relevant parties must be notified immediately by displaying an alert somewhere in the webpage. In previous projects, the browser would poll the server for any relevant changes every N seconds.
I have been reading on web sockets and think this is the prefect solution for this problem (I do not like polling).
I have some questions regarding Web Sockets in JavaEE. Please correct me if I am wrong.
Web Socket seems to be supported on Glassfish server not in latest version of JBoss/Wildfly.
If 1000 clients are logged in and connected to the server using Web Socket, does the server have 1000 separate sockets open for each connection? Or is the implementation similar to Node.js where a single server socket is used for all client connections. This information does not seem to be documented anywhere in JavaEE tutorials.
Websockets are TCP connections and the websocket protocol is simply an upgrade of the TCP protocol with an handshake procedure similar to the http protocol, but the websocket protocol is bidirectional.
I don't think you are getting a single web socket in Node.js. You have a connection per logged client anyway. In Node.js you have the broadcast, but this is the same as sending a message to any logged client through the related web socket. You have the same functionality in glassfish, where you simply loop on all the web sockets:
http://www.byteslounge.com/tutorials/java-ee-html5-websockets-with-multiple-clients-example
and you can do the same in weblogic:
https://docs.oracle.com/middleware/1212/wls/WLPRG/websockets.htm#WLPRG872
This is the same as Node.js without any wrapper.

Can the same socket.io interface support traditional socket connection as well as websocket connection

I have to support a GPS devices that writes to a socket and at the same time websockets from suppose a mobile device. Can the same socket.io code support both of these?
Short answer : no such module for socket.io to call or serve TCP requests doesn't exist .
Long answer : you can easily solve this in your node.js by working with this possible options
First option would be to just support socket.io all the way client
and server. So you can easily develop with one protocol.
Second option is to combine socket.io + TCP server, and handle
received requests with the same handler. To simplify take this as an
example for server-side.
// run socket.io server
// run TCP server
function Handler(event,data,socket){
this.getProfileData=function(data,socket){
// things to do
// call socket.emit for socket.io ,or socket.write for TCP
}
this[event](data,socket); // call appropriate event
}
// when receiving a new message from either socket.io or TCP
new Handler(event,data,socket);
Third option is to use a bridge such as WebTCP or implement your own bridge.
There is also experimental TCP socket API for browsers, but its not recommended.

Resources