0

I am trying to invoke my serverless function, I am using the subset FaasD of the Open-FaaS serverless framework. I am running it on a raspberry pi model 4b with Raspbian OS Lite x64.

My serverless function publishes "Hello World" as an MQTT Message, and I have a local subscriber to receive the message.

This is my handler.py function:

import paho.mqtt.client as mqtt

# MQTT broker configuration.
BROKER = 'localhost'
PORT = 1883
TOPIC = 'topic'

def handle(req):
    """handle a request to the function
    Args:
        req (str): request body
    """
    
    # Create the MQTT client.
    client = mqtt.Client()

    # Connect to the broker and publish the request body to the topic.
    client.connect(BROKER, PORT, 60)
    client.publish(TOPIC, req)

    return f'Message published to topic {TOPIC}'

This is my local subscriber:

import paho.mqtt.client as mqtt

BROKER = 'localhost'
PORT = 1883
TOPIC = 'topic'

def on_connect(client, userdata, flags, rc):
    print(f'Connected: {rc}')
    client.subscribe(TOPIC)

def on_message(client, userdata, msg):
    print(f'Topic: {msg.topic} - Message: {msg.payload.decode()}')

client = mqtt.Client()

client.on_connect = on_connect
client.on_message = on_message

client.connect(BROKER, PORT, 60)

client.loop_forever()

I tried setting up a local publisher and subscriber, and that worked just fine. Every time I try to invoke our serverless function, we get:

ConnectionRefusedError: [Errno 111] connection refused

Agro20
  • 1
  • 1

1 Answers1

0

OpenFaaS functions run in their own container, so localhost points to the container, not the host system.

You need to

  1. Ensure that mosquitto is configured to accept connections on all interfaces
  2. Use the external IP address of the host system to connect to the broker.
hardillb
  • 54,545
  • 11
  • 67
  • 105