so messages can be pushed into the queue from dashboard again. in my consumer Worker, I want to rou

so messages can be pushed into the queue from dashboard again.

in my consumer Worker, I want to route the queue message back to a client using a websocket. I'm not having much luck.

async queue(batch: MessageBatch<any>, env: Env): Promise<void> {
        
        try {
            
            let messages = JSON.stringify(batch.messages);
            console.log(`consumed from our queue: ${messages}`);

            if(serverSocket !== undefined){
                console.log('ServerSocket=>', serverSocket) ;
                serverSocket.send(messages);

            }else{
                console.log('ServerSocket Undefined=>', serverSocket) ;

                console.log('Sending from Running Sock=>', serverSocket) ;
                env.runningSock.send(messages);
            }

        } catch (error) {
            console.log('Error=>',error);
        }
      },


My socket connects correctly and produced test messages etc - but as soon as the queue has a message, the socket is null. I've tried putting the server object from the WebsocketPair into a variable both local and in the env object. Always null.

How can I store the active websocket from the worker, so the queue message can be send ?
Was this page helpful?