Search code examples
pythonnode.jswebsocketnode-red

Connecting Python to a webpage via node-red


I am trying to take info from my python program and update this real time on the web page.

I am trying to use node-red and communicate via web sockets.

My python program is below:

#!/usr/bin/python

import time

import websocket
ws = websocket.WebSocket();
ws.connect("ws://localhost:1880/ws/example")
count = 0;
while(count < 50):
        print "Sending 'Hello, World'..."
        ws.send("Hello, World")
        print "Sent"
        time.sleep(5)
        count = count + 1

 ws.close()

Using Node-red I have set up my flow as follows:

Node-Red Flow

However when I run them both, my python program says it is sending the message, however the node red console is returning null for the msg value.


Solution

  • Please check your settings.js file -- do you have a url prefix defined for either httpRoot or httpNodeRoot?

    For instance, in my project, when I add a new websocket config node, this info box is shown:

    By default, payload will contain the data to be sent over, or received from a websocket. The listener can be configured to send or receive the entire message object as a JSON formatted string. This path will be relative to /red.

    If so, I believe you will have to modify the url in your python code, like so: ws.connect("ws://localhost:1880/red/ws/example")

    substituting your prefix, of course...