PubNub unsubscribe isn't firing presence event - javascript

I'm using the latest JavaScript API for PubNub, with presence switched on in the admin interface. I'm running two clients on a channel, initialized and subscribed like this:
pubnub = PUBNUB.init({
publish_key : 'xxx',
subscribe_key : 'xxx',
origin : 'pubsub.pubnub.com',
cipher_key : 'xxx',
ssl : 'true',
uuid : uuid
});
pubnub.subscribe({
channel : CHANNEL,
callback : function (message) {
$("#box").val($("#box").val() + message + "\r\n");
},
connect: function () { console.log("Connected"); users(); },
disconnect: function () { console.log("Disconnected"); },
reconnect: function () { console.log("Reconnected"); },
error: function () { console.log("Network Error"); },
presence: function (m) { console.log("Presence: " + m); }
});
I unsubscribe from the channel like this:
function unsubscribe() {
pubnub.unsubscribe({
channel: CHANNEL
});
console.log("Unsubscribed");
};
When clients join the channel, I see join presence actions in all clients that are subscribed. When clients timeout, I see those actions too.
When I call unsubscribe() the log line prints, and that client does not receive any more messages published on the channel, but there is no leave presence action received by other clients who are still subscribed. What am I doing wrong?

Strangely enough, when I tried to subscribe this morning I got a 400 Invalid error. When I investigated, the PubNub admin screen told me that there were no keys associated with my project.
When I had regenerated the keys I could subscribe - and the join and leave events worked as expected too. I now get the response I expect from here-now as well.
Odd, but fixed!

Related

How to listen to custom event dispatched by third party library in VUE JS

I am implementing agora chat in vue js where there is object which emits 6 events. How can I listen to those in VUE JS. Here is its code in core javascript:
this.agora.conn = new AC.connection({
appKey: this.agora.appKey,
});
this.agora.conn.addEventHandler("connection&message", {
// Occurs when the app is connected to Agora Chat.
onConnected: () => {
console.log("App connected.");
},
// Occurs when the app is disconnected from Agora Chat.
onDisconnected: () => {
console.log("App disconnected.")
},
// Occurs when a text message is received.
onTextMessage: (message) => {
console.log("Message Recieved: ",message);
},
// Occurs when the token is about to expire.
onTokenWillExpire: (params) => {
console.log("Token is about to expire. ",params)
this.refreshToken(this.agora.username, this.agora.password);
},
// Occurs when the token has expired. You need to get a token from your app server to log in to Agora Chat.
onTokenExpired: (params) => {
console.log("Token is expired. ",params)
this.refreshToken(this.agora.username, this.agora.password);
},
onError: (error) => {
console.log("on error: ", error);
},
});
Here agora is the data member in the component and this above code is in mounted hook.
I have noticed console the SDK is dispatching the events but this listener is not catching any. Here is agora data member:
agora : {
baseUrl : "https://a41.chat.agora.io",
appKey : "******",
username : "fana",
password: "123",
conn : null
}

Cancel grpc call from client on server streaming

I am using nodejs grpc server streaming to deliver realtime data from server to the clients. As the clients want to listen to the data, they should call a server function which returns a server stream. Then if the client want to end the listening I guess it should call the end method on the call object. But unfortunately I couldn't find any clue on the documentation about how to do this. I have tried call.end and call.destroy and call.cancel but end and destroy did nothing and cancel throws the following error. The sample client side code is as following:
function getData(token, userId) {
const data = {
token,
userId
}
let call = DataService.getData(data)
call.on('data', res => {
console.log(res)
})
call.on('status', console.log);
call.on('error', console.log);
setTimeout(() => {
console.log('destroy')
call.cancel()
}, 5000)
}
and the error is:
Error: 1 CANCELLED: Cancelled on client
at Object.callErrorFromStatus (...grpc\node_modules\#grpc\grpc-js\build\src\call.js:31:26)
at Object.onReceiveStatus (...grpc\node_modules\#grpc\grpc-js\build\src\client.js:330:49)
at Object.onReceiveStatus (...grpc\node_modules\#grpc\grpc-js\build\src\client-interceptors.js:299:181)
at ...grpc\node_modules\#grpc\grpc-js\build\src\call-stream.js:145:78
at processTicksAndRejections (internal/process/task_queues.js:75:11) {
code: 1,
details: 'Cancelled on client',
metadata: Metadata { internalRepr: Map(0) {}, options: {} }
}

How to make 2 users join the same room in socket.io for 1 to 1 chat?

I was looking at answers here How to send a message to a particular client with socket.io, specifically the 2nd one, which recommends using rooms for 1 to 1 chat. I did the following on my server:
io.on('connection', (socket) => {
socket.on('message', (data) => {
socket.join(data.to);
io.sockets.in(data.to).emit('send_message', { message: data.message, to: data.to });
});
});
And on my client I have this:
const sendMessage = (e) => {
e.preventDefault();
io.emit('message', { to: friend._id, from: currentUserID, message }); // here friend._id
// will be currentUserID in the 2nd user's browser. Which makes it so they join different rooms
setMessage('');
}; // this function runs on form submission.
io.on('send_message', (message) => {
// do stuff with the message
});
But my users won't join the same room. Each one joins a room identified by the other user's id (friend._id). So they're unable to see each other's messages. In the answer from above they do a similar thing, just with each user's emails instead, which should result in the same issue I have as far as I understad.

Twilio voice call works only once [microphone permission issue]

I implemented Twilio voice call functionality that enables users to call support people from the browser but it works the first time only after allowed microphone permission but then next time getting below error and resetting microphone permission then voice call works.
code: 31000, message: "Cannot establish connection. Client is disconnected"
Below is code snippet on the client-side that written in angular with help of twilio client docs
import twilio from 'twilio-client';
public device: any;
this.device = new twilio.Device('<token-fetched>', {
codecPreferences: ['opus', 'pcmu'],
fakeLocalDTMF: true,
enableIceRestart: true
})
let params = {
To: '<to-number>',
Id: '<id>',
token: '<token-fetched>'
}
if (this.device) {
this.device.connect(params);
}
this.device.on('error', (error) => {
console.log("this is error",error);
})
this.device.on('disconnect',(connection) => {
console.log("connection ended", connection);
})
I had the same issue here, the problem is that I was calling var connection = Twilio.Device.connect(); before the async setup work had been completed.
Write device.connect inside of device.ready function like
Twilio.Device.ready(function(device) {
console.log('Ready');
var connection = Twilio.Device.connect();
// Do rest of twilio work ...!
}
This will fix your issue.

faye authentication with token

I am working with faye messaging system and I want to add authentication! I worked my way through the website and followed the tutorial.
On the Client I have an extension for outgoing Messages:
var droneFaye = new faye.Client("/faye", {
timeout: 120
});
var USER_TOKEN = 'ifd63cylqwsyaq9c2ptzywjujgtfpxs';
droneFaye.addExtension({
outgoing: function(message, callback) {
if (message.channel !== '/meta/subscribe')
return callback(message);
message.ext = message.ext || {};
message.ext.token = USER_TOKEN;
console.log(message);
callback(message);
}
});
On the Server:
var token = 'ifd63cylqwsyaq9c2ptzywjujgtfpxs'
var serverAuth = {
incoming: function(message, callback) {
// Let non-subscribe messages through
if (message.channel !== '/meta/subscribe')
return callback(message);
// Get subscribed channel and auth token
var msgToken = message.ext && message.ext.token;
// Find the right token for the channel
if (token !== msgToken) {
message.error = 'Invalid subscription auth token';
}
console.log(message);
callback(message);
}
};
var adapter = new faye.NodeAdapter({
mount:'/faye',
timeout:45
});
adapter.addExtension(serverAuth);
adapter.attach(httpServer);
I have a subscription on the server like this:
adapter.getClient().subscribe("/drone/move", function(cmd) {});
Alright! So when I start the server and there is NO CLIENT it already calls the extension for the subscriptions and it will output on the console:
{ channel: '/meta/subscribe',
clientId: '2isdeb0ds77zl0lh82ob1kqu29y1cajnv80',
subscription: '/drone/move',
id: '2',
error: 'Invalid subscription auth token' }
Once a Client connects to the server it will again call the extension and it will output this:
{ channel: '/meta/subscribe',
clientId: '3kechs0c7smpc05z5o7d0a8qcd301d8zi41',
subscription: '/drone/move',
id: '3',
ext: { userId: 18787, token: 'ifd63cylqwsyaq9c2ptzywjujgtfpxs' } }
So this looks good! But no other messages are going to arrive on the server even though they have the correct token and there is no error message!
Just for information. If you add a error key with a value to the message object it will not pass the message to its subscription...its supposed to be like that!..
Also when I comment out the message.error in the extension it works fine but of course there is not authentication.
So does anyone know why the server calls the extension even though there is no client and second why does faye not give the message to its subscription even though there is no error in the message object?
thx!
Question was also asked, and answered here:
This is caused by the call to adapter.getClient().subscribe().
[...] the server-side client (<adapter>.getClient()) returns a connection to the server, thus walks through the same extensions (incoming, outgoing) as the client does.
[...] Attach an outgoing extension and you'll see it being responded to the server-client.

Categories

Resources