webdis icon indicating copy to clipboard operation
webdis copied to clipboard

XREAD not receiving messages via websocket

Open gmussi opened this issue 2 years ago • 0 comments

Hi there,

The following code DOES NOT receive messages when XADD is used:

const wsEndpoint = "ws://localhost:7379/.json";
const socket = new WebSocket(wsEndpoint);

socket.onopen = function(event) {
  console.log("WebSocket connection established");
  socket.send(JSON.stringify(["XREAD", "BLOCK", "0", "STREAMS", "test.mystream", "$"]));
  console.log("XREAD SENT");
};
socket.onmessage = function(event) {
  const message = JSON.parse(event.data);
  console.log("WebSocket message received:", message);
}

By comparison, the following code DOES WORK when XADD is used:

var previous_response_length = 0
xhr = new XMLHttpRequest()
xhr.open("GET", `http://localhost:7379/XREAD/BLOCK/0/STREAMS/test.mystream/$`, true);
xhr.onreadystatechange = checkData;
xhr.send(null);

function checkData() {
  if(xhr.readyState == 3)  {
    response = xhr.responseText;
    chunk = response.slice(previous_response_length);
    previous_response_length = response.length;
    console.log("chunk reading", chunk);
    }
  };

I am running the following on my docker-compose.yml:

webdis:
    image: nicolas/webdis:latest
    command: /usr/local/bin/webdis /config/webdis.json
    environment:
      REDIS_HOST: redis
      REDIS_PORT: 6379
    volumes:
      - ./docker/webdis/webdis.json:/config/webdis.json
    depends_on:
      - redis
    ports:  # allow connections from the Docker host on localhost, port 7379
      - "7379:7379"

Also, here is my webdis.config file:

{
	"redis_host":	"redis",

	"redis_port":	6379,
	"redis_auth":	null,

	"http_host":	"0.0.0.0",
	"http_port":	7379,

	"threads":	5,
	"pool_size": 20,

	"daemonize":	false,
	"websockets":	true,

	"database":	0,

	"acl": [
		{
			"disabled":	["DEBUG"]
		},

		{
			"http_basic_auth":	"user:password",
			"enabled":		["DEBUG"]
		}
	],

	"verbosity": 4,
	"logfile": "/dev/stderr"
}

The following line is printed on webdis when websocket is attempted: [1] 03 May 08:19:42 D WS: /XREAD/BLOCK/0/STREAMS/test.mychannel/$

Additional information:

  • Using the same XREAD command using Rediscmd, redis-cli or regular nodejs code with redis adapter, works in all scenarios.

After a lot of trial-and-error, I did not manage to make websockets work for XREAD. Any advice would be greatly appreciated.

gmussi avatar May 03 '23 08:05 gmussi