Simple file sender / receiver over Event Broker

Simple cURL REST POST and python MQTT receiver to move a file across solace.

Curl Sender

  1. Create directory with files for sending: ‘sender’
  2. Add password to ‘restpass’ variable
resthost=mr1nljqp0y2dox.messaging.solace.cloud
restport=9000
restuser=solace-cloud-client
restpass=password
filemask=./sender/*

for filepath  in $filemask
do
  # List all the files in subdirectory
  file=$(basename $filepath)

  curl -v -u $restuser:$restpass \
     -X POST http://$resthost:$restport/file/upload/binary/$file \
     -H "Solace-User-Property-File-Name: '"$file"'" \
     -H "Content-Type: application/octet-stream" \
     --data-binary @$filepath
done

Python MQTT Listener to file

  1. Create directory ‘receiver’
  2. add password to username_pw_set
import paho.mqtt.client as mqtt
import os

def on_log(client, userdata, level, buf):
    print("log: ",buf)

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe("file/#")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    #print(msg.topic)
    #print("")
    filename="receiver/" + os.path.basename(msg.topic)
    print("Writing:"+filename)
    f=open(filename,"wb")
    f.write(msg.payload)
    f.close()

client = mqtt.Client()
client.on_log=on_log
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(username="solace-cloud-client", password="password")
client.connect("mr1nljqp0y2dox.messaging.solace.cloud", 1883, 60)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
client.loop_forever()

This is great @Phil ! Thanks for sharing!

That’s a good one. Do we have receiver for REST as well ?

@Naga - Do you want Solace to PUSH the file to a remote web server, or are you looking to PULL a file?

I am looking for solace to push the file where a REST consumer is connected to Solace.

Hi Naga,
You are best to use the Javascript API inside of your rest consumer. There is a simple sample here: