stomp.js cannot receive message sent from pika (RabbitMQ version: 3.11.7) - rabbitmq

I have a web page that should receive messages from RabbitMQ using STOMP:
<body>
<script src="stomp.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sockjs-client#1.1/dist/sockjs.min.js"></script>
<script>
var client = Stomp.client('ws://localhost:15674/ws');
client.debug = null;
var sub = function(d) {
// print_first(d.body);
console.log("got the message! ", d.body)
}
var on_connect = function(x) {
id = client.subscribe("/topic/test", sub);
console.log("connected")
};
var on_error = function() {
console.log('error');
};
client.connect('guest', 'guest', on_connect, on_error, '/');
</script>
</body>
when I run this code, it shows connected in the console (so far so good)
I also have a python backend, which should send messages to the queue (send.py):
import pika
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare("/topic/test")
properties = pika.BasicProperties(
content_type='application/json',
content_encoding='utf-8',
delivery_mode=2,
)
channel.basic_publish(exchange='',
routing_key='/topic/test',
body='Hello World!',
properties=properties)
The messages are sent (I ran the script with py -m send; the messages appear in the RabbitMQ Management) :
However the console.log in sub isn't running. Any idea how I can fix this?
Thanks!

Ended up using stomp.py instead of pika:
import stomp
PORT = 61613
LOCALHOST = '0.0.0.0'
conn = stomp.Connection11([(LOCALHOST, PORT)])
conn.connect('guest','guest')
conn.send(body="start",destination='/queue/test')
conn.send(body="end",destination='/queue/test')
conn.disconnect()

Related

asyncio stream vs synchronous stream in socket communication with a react native app

Objective is esp32 running micropython acts as a server while android app acts as a client. Before asyncio stream I am able to communicate successfully, but after switching to asyncio i fail to do so, only android app to esp32 is successful but app is failing to retrieve json output from server and I even tried text strings too . App side code remains unchanged for both synchronous/asyncio codes.
Desired output:
response = {
'error': 'invalid request',
'status': 'retry'
}
synchronous side:
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: application/json\n')
conn.send('Connection: close\n\n')
conn.sendall(ujson.dumps(response ))
asyncio side:
swriter.write(ujson.dumps(response ))
await swriter.drain()
react native side:
fetch( 'http://192.168.0.110' )
.then(response => response.json())
.then((responseJson) => {
const data1 = responseJson;
console.log('getting data from fetch', data1)
setData({ data1 });
onConnectionMessage(data1);
})
synchronous way I was able to retrieve the json output sent from esp32 to android app(react native), but the same code using asyncio failed. What am I doing wrong?
sample asyncio server side code is:
import usocket as socket
import uasyncio as asyncio
import uselect as select
import ujson
from heartbeat import heartbeat # Optional LED flash
class Server:
def __init__(self, host='0.0.0.0', port=80, backlog=5, timeout=10):
self.host = host
self.port = port
self.backlog = backlog
self.timeout = timeout
async def run(self):
print('Awaiting client connection.')
self.cid = 0
asyncio.create_task(heartbeat(100))
self.server = await asyncio.start_server(self.run_client, self.host, self.port, self.backlog)
while True:
await asyncio.sleep(100)
async def run_client(self, sreader, swriter):
self.cid += 1
print('Got connection from client', self.cid)
try:
while True:
try:
res = await asyncio.wait_for(sreader.readline(), self.timeout)
except asyncio.TimeoutError:
res = b''
if res == b'':
raise OSError
print('Received {} from client {}'.format(ujson.loads(res.rstrip()), self.cid))
response = {
'error': 'invalid request',
'status': 'retry'
}
swriter.write(ujson.dumps(response))
await swriter.drain() # Echo back
except OSError:
pass
print('Client {} disconnect.'.format(self.cid))
await sreader.wait_closed()
print('Client {} socket closed.'.format(self.cid))
async def close(self):
print('Closing server')
self.server.close()
await self.server.wait_closed()
print('Server closed.')
server = Server()
try:
asyncio.run(server.run())
except KeyboardInterrupt:
print('Interrupted') # This mechanism doesn't work on Unix build.
finally:
asyncio.run(server.close())
_ = asyncio.new_event_loop()
got the error: asyncio.wait_for(sreader.readline(), self.timeout)------> changed to
asyncio.wait_for(sreader.read(2048), self.timeout). Now client is recieving json output immediately after closing the socket

publishing events to pubnub blocks from javascript client

I wanted to try out pubnub BLOCKS with a very contrived example.
Essentially, I'm publishing a simple message from a client (using javascript sdk) to see if the BLOCK that I've set (or i "think" i've set) to that respective channel is listening...contrived example is failing thus far...
Steps
Create an APP in pubnub to generate credentials.
Included pubnub SDK in simple HTML file, initialized Pubnub, set event listener, and publish/subscribe methods. Set channel to 'hello-world'
2a. Published/Subscribed to messages successfully from different browser windows with .
Went to pubnub debug console and set channel as 'hello-world' to see if messages from 'hello-world' channel would be broadcast and they were not.
From the client, I console logged the object that is returned from messages and the channel is appearing as 'hello-world'..so this left me wondering, why aren't i seeing the messages registered in the pubnub debug console in the same hello-world channel ?
Particularly, my question is: how can i send messages to a pubnub BLOCK from a pubnub CLIENT and send messages from a pubnub BLOCK to a pubnub CLIENT ? or in other words, pub/sub a BLOCK with a CLIENT using the Javascript SDK ?
The simple.js for hello-world example code:
(function(){
var pubnub = new PubNub({ publishKey : 'p-key', subscribeKey : 's-key' });
function $(id) { return document.getElementById(id); }
var box = $('box'), input = $('input'), channel = 'hello-world';
pubnub.addListener({
message: function(obj) {
box.innerHTML = (''+obj.message).replace( /[<>]/g, '' ) + '<br>' + box.innerHTML
}});
pubnub.subscribe({channels:[channel]});
input.addEventListener('keyup', function(e) {
if ((e.keyCode || e.charCode) === 13) {
pubnub.publish({channel : channel,message : input.value,x : (input.value='')});
}
});
})();
Publishing Messages and Events to PubNub BLOCKS from JavaScript
I've created an example below which sends messages to PubNub.
You can register a BLOCK on the hello-world channel to catch the message.
Register a BLOCK on PubNub. Make sure to start/deploy the block.
Update the publishKey and subscribeKey in the example below.
Run the example below.
(()=>{
'use strict';
// Initialize PubNub Socket SDK
const pubnub = new PubNub({
publishKey : 'demo'
, subscribeKey : 'demo'
});
// GUI Elements
const box = $('#messages')
, input = $('#message')
, submit = $('#submit')
, channel = 'hello-world';
// Open Socket to Channels
pubnub.subscribe({ channels : [channel] });
// When Messages Arrive
pubnub.addListener({ message: obj => receive_chat(obj) });
// When user sends chat
submit.click( event => send_chat(input.val()) );
input.keyup( event => {
if ((event.keyCode || event.charCode) === 13)
return send_chat(input.val());
});
// Draw Chat Messages on Screen
function receive_chat(obj) {
box.html((''+obj.message).replace( /[<>]/g, '' )+'<br>'+box.html());
}
// Send Chat Message
function send_chat(message) {
console.log(input.val());
pubnub.publish({ channel : channel, message : message });
input.val('');
return false;
}
})();
div, input { font-family: "Lucida Grande","Lucida Sans Unicode","Lucida Sans",Geneva,Arial,sans-serif; }
input { padding: 10px; margin: 10px; }
input[type=submit] { width: 100px; line-height: 100px; font-size: 20px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.3.min.js"></script>
<input id="message" placeholder="type your message">
<input id="submit" type="submit" value="Send">
<div id="messages"></div>

Unable to emit after rabbitmq channel.start_consuming() call in flask-socketio handler

I'm trying to listen to a rabbitmq queue from within a flask-socketio event handler so I can send realtime notifications to a web app. My setup so far:
Server
import pika
import sys
from flask import Flask, request
from flask_socketio import SocketIO, emit, disconnect
app = Flask(__name__)
app.config['SECRET_KEY'] = 'not-so-secret'
socketio = SocketIO(app)
def is_authenticated():
return True
def rabbit_callback(ch, method, properties, body):
socketio.emit('connect', {'data': 'yes'})
print "body: ", body
#socketio.on('connect')
def connected():
emit('notification', {'data': 'Connected'})
creds = pika.PlainCredentials(
username="username",
password="password")
params = pika.ConnectionParameters(
host="localhost",
credentials=creds,
virtual_host="/")
connection = pika.BlockingConnection(params)
# This is one channel inside the connection
channel = connection.channel()
# Declare the exchange we're going to use
exchange_name = 'user'
channel.exchange_declare(exchange=exchange_name,
type='topic')
channel.queue_declare(queue='notifications')
channel.queue_bind(exchange='user',
queue='notifications',
routing_key='#')
channel.basic_consume(rabbit_callback,
queue='notifications',
no_ack=True)
channel.start_consuming()
if __name__ == '__main__':
socketio.run(app, port=8082)
Browser
<script type="text/javascript" charset="utf-8">
var socket = io.connect('http://' + document.domain + ':8082');
socket.on('connect', function(resp) {
console.log(resp);
});
socket.on('disconnect', function(resp) {
console.log(resp);
});
socket.on('error', function(resp) {
console.log(resp);
});
socket.on('notification', function(resp) {
console.log(resp);
});
</script>
If I comment out the "channel.start_consuming()" line at the bottom of the server code and load the browser page, I connect successfully to flask-socketio and I see {data: "Connected"} in my console.
When I uncomment the line, I do not see {data: "Connected"} in my console. Nevertheless, when I send a message to the notifications queue, the rabbit_callback function fires. I see my message printed to the server console, but the emit call doesn't seem to work. There are no errors on the server or in the browser. Any advice is much appreciated.
Thanks!
I had the same problem using eventlet and I just solved adding:
import eventlet
eventlet.monkey_patch()
,at the beginning of my source code.
Anyway my code is a bit different and using the start_background_task method:
import pika
from threading import Lock
from flask import Flask, render_template, session, request, copy_current_request_context
from flask_socketio import SocketIO, emit, join_room, leave_room, \
close_room, rooms, disconnect
app = Flask(__name__, static_url_path='/static')
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode=async_mode)
thread = None
thread_lock = Lock()
#socketio.on('connect', namespace='/test')
def test_connect():
global thread
with thread_lock:
if thread is None:
thread = socketio.start_background_task(target=get_messages)
emit('my_response', {'data': 'Connected', 'count': 0})
print('connected')
def get_messages():
channel = connect_rabbitmq()
channel.start_consuming()
def connect_rabbitmq():
cred = pika.credentials.PlainCredentials('username', 'password')
conn_param = pika.ConnectionParameters(host='yourhostname',
credentials=cred)
connection = pika.BlockingConnection(conn_param)
channel = connection.channel()
channel.exchange_declare(exchange='ncs', exchange_type='fanout')
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='myexchangename', queue=queue_name)
channel.basic_consume(callback, queue=queue_name, no_ack=True)
return channel
Hope this helps...

instant messaging with intel xdk receiving notification on new message

i would like to create instant messaging in cross platform. how can i get the application keep listening to the server so when there is a message coming, the application could receive a notification.
maybe like service in android?
I've read about push message (push mobi) but it doesn't seem to meet my need since it blast the notification on all registered id from admin panel, not from 1 id to another id.
i notice GCM but some say it is not suitable for sending and receiving chat.
Sounds like a good scenario for websockets. There's a phongap plugin for android that will allow you to use them.
Take a look at the plugin demo. It looks pretty strait-forward.
client side javascript:
var socket = io.connect("http://10.0.2.2:8080");
document.getElementById('log').innerHTML = "connecting";
socket.on('ping', function (data) {
document.getElementById('log').innerHTML = data.message;
socket.emit('pong', { message: 'Hello from client!' });
});
socket.on('connect', function () {
document.getElementById('log').innerHTML = "connected";
});
});
Server side web service in node.js:
var io = require('socket.io').listen(8080);
io.sockets.on('connection', function (socket) {
console.log('emit...');
socket.emit('ping', { message: 'Hello from server ' + Date.now() });
socket.on('pong', function (data) {
console.log(data.message);
});
});
console.log('listening on port 8080');

Socket.io-objc 400 (Handshake Error) Remote Server vs. localhost

When I connect to Socket.io on the server I get a 400 error, but I don't see any errors connecting to localhost. (same code and connecting via socket.io-objc)
I'm using Azure to host the node.js project.
(I also have websockets on in the azure config if that makes a difference)
ERROR: handshake failed ... The request timed out.
onError() Error Domain=SocketIOError Code=-6 "The operation couldn’t be completed.
(SocketIOError error -6.)" UserInfo=0x1874cc00 {NSUnderlyingError=0x1870cad0 "The request timed out."}
Server Code (On Azure)
var fs = require('fs');
var app = require('express')(),
server = require('http').createServer(app),
redis = require("redis"),
Primus = require('primus'),
kue = require("kue");
var haversine = require('haversine')
var finish = require("finish");
var client = redis.createClient(12276, "redis url");
client.auth('password');
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
socket.emit('join', { status: 'connected' });
});
var port = process.env.port || 1337;
server.listen(port);
SOCKET.IO-OBJC CODE
- (void) reconnect
{
[socketIO disconnectForced];
socketIO = [[SocketIO alloc] initWithDelegate:self];
socketIO.useSecure = NO;
[socketIO connectToHost:#"siteurl" onPort:80];
}