Skip to content

Instantly share code, notes, and snippets.

Last active August 14, 2019 01:16
  • Star 14 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Simple example of subscribing to Rancher change events
npm install ws
Create an API key in Rancher and start up with:
node socket.js address.of.rancher:8080 access_key secret_key project_id
var WebSocket = require('ws');
var host = process.argv[2];
var accessKey = process.argv[3];
var secretKey = process.argv[4];
var projectId = process.argv[5];
var url = 'ws://'+accessKey+':'+secretKey+'@'+host+'/v1/projects/'+projectId+'/subscribe?eventNames=resource.change';
var socket = new WebSocket(url);
socket.on('open', function() {
console.log('Socket opened');
socket.on('message', function(messageStr) {
var message = JSON.parse(messageStr);
if ( === 'ping' )
else if ( === 'resource.change' && )
var resource =;
var info = 'name=' + ', state='+resource.state;
if ( resource.transitioning !== 'no' )
info += ', transitioning='+resource.transitioning + ', message='+resource.transitioningMessage
console.log(message.resourceType, message.resourceId, 'changed:', info);
socket.on('close', function() {
console.log('Socket closed');
Copy link

arkka commented Aug 15, 2016

Hi @vincent99,

I'm trying to consume this websocket for my python script. i'm still getting 401 response which means unauthorized access. Is it appending username:password (basic auth) enough?

Here is my url

url = 'ws://' + self.api.access_key + ':'+self.api.secret_key+'@' + ':' + str(self.api.port) + '/v1/projects/1a170/subscribe?eventNames=resource.change

I've also tried using Chrome websocket plugin to debug this. If I logged on rancher UI, I'm able to subscribe the websocket. But, if i'm logged out from the Rancher UI - using basic auth it doesn't work.

Follow up question, are there any (detailed) documentation for this websocket? I've tried to get sense of it, and found some interesting query variable from the rancher UI, such as include=hosts&include=services&include=instances&include=instance&include=instanceLinks&include=ipAddresses

Copy link

arkka commented Aug 15, 2016

NVM, i got it working!

It seem for python (or in my case), I'm required to login first using requests , retrieve the authorization response, then use it on websocket call.

 ws = websocket.WebSocketApp(url,

Copy link

@arkka The UI does use token auth, but basic with API keys works too.. They python library probably just doesn't support parsing ws://user:pass@host/path and turning that into the Authorization header ("Authorization: Basic " + base64encode("user:pass")).

?include= is sort of a hack put in for/used by the UI and is explicitly undocumented because it will probably be removed in a future API revision.

There is one other event name (eventNames=service.kubernetes.change) and that's about it.

Copy link

arkka commented Aug 15, 2016

Exactly that's the problem @vincent99. It's seems the python library for websocket-client did not support it, and using base64 as my authorization token on header fix everything.

Thank you 👍

Copy link

alfrye commented Dec 6, 2018

I am using rancher 2.11 and after I get a few pings back from rancher the websocket closes with error code 1006. Any ideas?

I accessing the websocket from a node.js server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment