nchan icon indicating copy to clipboard operation
nchan copied to clipboard

Publish on websocket handshake

Open metatarz opened this issue 5 years ago • 3 comments

In my application, clients open up a new websocket connection by simply instantiating it i.e new Websocket(pubEndpoint); whenever they visit my website and close it down when navigating away or simply closing the tab. I use nginx as a reverse proxy to perform additional server-side logic, and nchan's role is to deal with websocket publish events. My question is: Is there a way to make nchan publish when opening/closing a websocket? My nginx configuration file is the following:

 location ~ /pub/(\w+)$ {
        nchan_publisher;
        nchan_channel_id $1;
        nchan_publisher_upstream_request /log;
    }

    location ~ /sub/(\w+)$ {
        nchan_subscriber;
        nchan_channel_id $1;

    }

    location /log {
        proxy_pass http://webapp;
        proxy_set_header X-Original-Host $host;
        proxy_set_header X-Channel-Id $nchan_channel_id;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Connection $http_connection;  
        proxy_set_header Upgrade $http_upgrade;  


    }

metatarz avatar Apr 22 '20 19:04 metatarz

Is there a way to make nchan publish when opening/closing a websocket?

Not sure I understand what exactly do you mean when you say make nchan publish...

If I'm not mistaken, you want some action to happen on opening/closing a websocket In that case https://nchan.io/#subscriber-presence is what you need.

ivanovv avatar Apr 23 '20 12:04 ivanovv

Yes, subscriber presence works for me. What would be the equivalent of a 'publisher presence'? In accordance with the docs, nchan_publisher_upstream_request would do the job but as I checked, it does not upstream requests unless a message is published.

metatarz avatar Apr 24 '20 14:04 metatarz

You have two options:

  1. convert your pub endpoint to pubsub https://nchan.io/#pubsub-endpoint (though I'm not sure whether callbacks work the same way for pubsub as they do for sub
  2. connect to sub endpoint at the same time with connecting to pub endpoint

ivanovv avatar Apr 24 '20 16:04 ivanovv