Websocket & N8N

Hi all,

I would like to do a websocket connection with N8N.
I think it’s possible because of : Is it possible to create a websocket connection within n8n?

But I have this error message : “VMError: Cannot find module ‘socket.io-client’”

How I can import socket.io-client ?
There is some configuration to do with Nginx ?

Thx for reply

  • n8n version: 1.18.2
  • Database (default: SQLite): Default
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker with Nginx
  • Operating system: Debian

Hey @tzana,

If you are trying to use that package you will need to create a custom docker image that includes it and make sure you have the env options set to allow it to be used, There are examples on the forum on how you can do this if needed.

1 Like

Hi @Jon

Thanks for you reply.

That I understand in my research : Add npm module and python to docker-compose instance
I have to make a docker file with the npm installation.
I’m using a docker run :

sudo docker run -it --rm \
--name n8n \
-p 5678:5678 \
-e WEBHOOK_URL="xxx" \
-e GENERIC_TIMEZONE="Europe/Paris" -e TZ="Europe/Paris" \
-e N8N_PAYLOAD_SIZE_MAX=2024 \
-v n8n_data:/home/node/.n8n \
n8nbackup

It’s possible to transform this to a docker file without loose my data ?

Hey @tzana,

That is just a docker run command rather than something that you would make a docker file, Luckily though as long as you continue to use -v n8n_data:/home/node/.n8n your data will be safe.

Hi Jon,

I created my custom n8n. My dockerfile :

FROM n8nio/n8n:latest

USER root
RUN npm install -g socket.io-client

ENV GENERIC_TIMEZONE="Europe/Paris"
ENV TZ="Europe/Paris"
ENV N8N_PAYLOAD_SIZE_MAX=2024
ENV NODE_FUNCTION_ALLOW_EXTERNAL="socket.io"

I build it :

 sudo docker build -t n8n-custom1302 .

And finaly, started my container:

sudo docker run -it --rm \
--name n8n \
-p 5678:5678 \
-e WEBHOOK_URL="xxx" \
-v n8n_data:/home/node/.n8n \
n8n-custom1302

Unfortunately I havent my data anymore, I need to create an account.

Have you some clues for me please ?

EDIT :

With, this I got my data

sudo docker run -it --rm \
--name n8n \
-p 5678:5678 \
-e WEBHOOK_URL="xxx" \
-v n8n_data:/home/node/.n8n \
docker.n8n.io/n8nio/n8n

Hey @tzana,

Your Dockerfile should be something like…

FROM n8nio/n8n:latest

USER root
RUN npm install -g socket.io-client
USER node

Then you can use the same run command as before making sure you use the new image

sudo docker run -it --rm \
--name n8n \
-p 5678:5678 \
-e WEBHOOK_URL="xxx" \
-e GENERIC_TIMEZONE="Europe/Paris" -e TZ="Europe/Paris" \
-e N8N_PAYLOAD_SIZE_MAX=2024 \
-v n8n_data:/home/node/.n8n \
your-image-name

But you will also need to set the env option NODE_FUNCTION_ALLOW_EXTERNAL to include the package you want to use or it still won’t work. Please don’t forget that this is considered an advanced feature so on top of the usual skill set required to run n8n you are also now making your own custom docker images so you will need to be comfortable with the different options around that as well.

Thank you Jon for your help :

I build a new custom image :

USER root
RUN npm install -g socket.io-client
USER node

ENV GENERIC_TIMEZONE="Europe/Paris"
ENV TZ="Europe/Paris"
ENV N8N_PAYLOAD_SIZE_MAX=2024

sudo docker build -t n8n-custom1302-4 .

And finally :

sudo docker run -it --rm \
--name n8n \
-p 5678:5678 \
-e WEBHOOK_URL="https://n8n.xxx/" \
-e NODE_FUNCTION_ALLOW_EXTERNAL=socket.io,socket.io-client \
-v n8n_data:/home/node/.n8n \
n8n-custom1302-4

I can import socket.io-client now :slight_smile:

1 Like

My final goal is to communicate with my Mattermost server, to create a chat Bot

Already, I’m doing websocket communication between Postman and my Mattermost server, all is working very well.

So thanks to this : https://community.n8n.io/t/is-it-possible-to-create-a-websocket-connection-within-n8n/33379/2and my custom N8n with socket.io-client import is ok

But, when I try to connect to my Mattermost WSocket, nothing happen : executing node loop

I think the request didnt go out. Maybe something to change in nginx ?

My nginx conf :

Thanks for the help

Hey @tzana,

The config looks ok to me for nginx but I am not an expert when it comes to nginx, The issue could also be with your code, Typically the code node is not used for running things that may require interaction so if you are expecting Mattermost to call back it may never happen.

Hello @Jon

So there is some solution to do websocket exchange with n8n ?
I tried with postman, is a success
I tried with a javascrit script, it’s ok too
So I would like to integrate this in N8n to make my bot Mattermost, miss only 1 thing : N8n :sweat_smile:

Hey @tzana

I have never tried to use websockets with n8n, I am not sure if any of our community members have managed to get it working though