nats websocket 使用教程

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

nats原生不支持websocket,我们使用了ws-tcp-relay代理,并使用websocket-nats客户端插件。

四川开发环境:

ws://四川开发:30225

nats://四川开发:30222

安装方法:

NPM

npm install websocket-nats

CDN

使用方式:

发布/订阅模型:

// Simple Publisher 
nats.publish('foo', 'Hello World!'); 

// Simple Subscriber 
nats.subscribe('foo', function(msg) { 
    console.log('Received a message: ' + msg); 
});

请求/响应模型:

// Request Streams 
var sid = nats.request('request', function(response) { 
    console.log('Got a response in msg stream: ' + response); 
});

// Replies 
nats.subscribe('help', function(request, replyTo) { 
    nats.publish(replyTo, 'I can help!'); 
});

队列模型:

// All subscriptions with the same queue name will form a queue group. 
// Each message will be delivered to only one subscriber per queue group, 
// queuing semantics. You can have as many queue groups as you wish. 
// Normal subscribers will continue to work as expected. 
nats.subscribe('foo', {'queue':'job.workers'}, function() { 
     received += 1; 
});

详细请进:websocket-nats

 

 

转载于:https://my.oschina.net/xainghu/blog/2870298

你可能感兴趣的:(nats websocket 使用教程)