I'm testing Pubnub 3.7.1. But I have a problem with the leave event trigger that doesn't fired.
I can only see the join and timeout trigger event. Here is some code that I use:
pubnub.subscribe({
channel: 'channel',
presence: manageUsers,
message: showMessage
});
function manageUsers(message, event, channel) {
console.log(message);
}
What could be the problem?
Thanks.
UDATE:
Another thing is when I enter in a channel where there are some people connected, I can't get their presence data. I can only get their presence data from new users.
Here is the example:
http://plnkr.co/edit/qlqhb677CZhTeR8Sa52x?p=preview
Your code works as expected.
When a user join, it triggers a 'join' action, and when the user goes idle, the action becomes 'timeout'.
The 'leave' action occurs when a user unsubcribe from the channel.
e.g.
byeButton.click(function(){
pubnub.unsubscribe({
channel : 'channel_1',
callback: function(m){
console.log(m.action); // should print 'leave'
}
});
});
See more at:
https://www.pubnub.com/docs/javascript/api/reference.html#unsubscribe
Related
Please, read to the end, help me!
I'm developing a multiroom game using SocketIO, with a client-side (React) and a server-side (Node). I'm having a problem on handling this situation:
Consider that at least two clients are inside a Room, after have joining it with socket.join(roomId) (at server-side).
Situation 1: When a player leaves the room manually (clicking a button in the page), the client-side does socket.emit("playerLeaving", someVar) to the server-side listener socket.on("playerLeaving", doStuff) that do some important things with the database (let's call it X listener). Then this listener fires back an event to the other client sockets in that room, with io.sockets.in(roomID).emit("playerLeft", someVar). No problem here.
Situation 2: Instead of manually leaving that room, that user just close the tab/browser, which fires a "disconnect" event do the server-side. Event in that case, the server must do the same procedure that he did on 1st situation, inside that X listener, so I just tried to emit this event socket.emit("playerLeaving", roomID) from inside the "disconnect" handler at server-side, so the X listener would receive it, do his stuffs, and broadcast to the other players that someone has left that room.
THE PROBLEM: the "disconnect" handler (at server-side) emits to X listener (also server-side), but this one doesn't receives it. Summin up, when a player in a room closes the tab/browser, the server isn't handling it correctly and consequently the other players in that room doesn't even get to know about that.
MY CODE:
SERVER-SIDE:
io.sockets.on('connection', (socket) => {
socket.on("playerJoiningRoom", (roomID) => {
socket.join(roomID); // to sign socket to a room
// .... some code
io.broadcast.to(roomID).emit("someoneJoined", someVar); // this works fine
})
// X-listener
socket.on("playerLeaving", (someVar) => {
// ... do stuff in database
io.sockets.in(roomID).emit("playerLeft", otherVar);
}
socket.on("disconnect", () => {
// ...some code here
// then, emit to the listener above
// example: socket.emit("playerLeaving", someVar);
});
})
}
CLIENT-SIDE
function socketEnteredRoom(socket, roomID) {
// notify server
socket.emit("playerJoiningRoom", roomID);
// listen to other players joining the room
socket.on("someoneJoined", someVar);
// listen to other players leaving the room. This listener works when some player leave manually (leave button), but don't work when a player closes tab/browser
socket.on("someoneLeft", someVar);
}
// when player wants to exit room
function handlerExitRoom() {
socket.emit("playerLeaving", someVar);
}
Waiting for your help! Thank you!
(sorry if this is a repeated question, but I couldn't find a answer that worked)
I'm not sure since I can't see your full code, but a common issue is people try to emit to rooms their socket is in in disconnect but the socket is no longer in any rooms at this point. Try changing to disconnect to disconnecting.
Try this,
//server-side
socket.on("disconnect", () => {
io.emit("playerLeaving", someVar);
});
//client-side
socket.emit("disconnect", () => {
});
So I've attached two events if someone screams, they are called synchronously, why so?
const events = require('events');
const eventEmitter = new events.EventEmitter();
eventEmitter.on('scream', function() {
console.log("Screaming");
});
eventEmitter.on('scream', function(name) {
console.log(name+" is screaming");
});
eventEmitter.emit('scream', 'Bob');
O/P:
Screaming
Bob is screaming
Because in nodejs, The event loop is single threaded and pick one event at a time and treat those events independently.
In your case, there are two event handler with the same name, so when event loop gets the eventEmitter.emit('scream', 'Bob') it sends the particular event handler.
When first event handler done with it, Now it goes to the second handler because with the same name.
It follow the FIFO but if you use emitter.prependListener(eventName, listener) then it will be executed first the FIFO.
You should know, if you want to call only one time then you should use eventEmitter.once('scream') It will be called only one time.
eventEmitter.once('scream', function() {
console.log("Screaming");
});
eventEmitter.emit('scream', 'Bob');
eventEmitter.emit('scream', 'Bob');
eventEmitter.emit('scream', 'Bob');
Output: Screaming // Only one time.
Because the Event loop fetches events from Event Queue and sends them to call stack one by one.
And Event Queue is FIFO (First-In-First-Out)
My bot (using MS BotFramework) is supposed to be hearing the conversation stream. If someone mentions 'chatbot' it should say 'Here I am!', otherwise stays quiet. It seems to be very simple and maybe it is but I am having a hard time trying to implementing it. Here is what I have:
bot.add('/', function(session) {
if (someoneSaidChatbot) {
session('Here I am!")
} else {
// session.reset(), maybe? No!
// session.endDialog() then? Uh...nope.
// nothing? Hmmm. negative
}
});
So, nothing works. If I leave there the bot just hangs and it stops listening to the stream or answering commands.
Any thoughts?
This code ends a dialog when someone types "chatbot" as part of the utterance. Is this what you are looking for?
bot.add('/', function (session) {
if (session.message.text.search("chatbot") >= 0) {
session.endDialog("Here I am");
}
});
I'd like to suggest use endConversationAction() to register a Bots Global Actions
bot.endConversationAction(
'enddialog', //dialog Id
'Here I am', //message
{ matches: /^.*chatbot/i } //match pattern
);
since this is global action, anytime when bot heard "Chatbot", it will say "Here I am" , if there are some dialog in stack, your proposed solution might not work.
It may also depend on which channel you're using. Some channels don't offer the ability for the Bot to listen to all of the messages in the conversation.
I am emitting events in the following order:
Socket.emit('waiting','waiting for someone');
Socket.emit('found','found someone');
On the client side, I'm listening to events like this:
socket.on('waiting',function(data)
{
alert(data);
});
socket.on('found',function(data)
{
alert(data);
});
But yet for some reason, I see an alert for 'Found someone' before 'waiting for someone'. Although when I look at the console, the events seem to be emitted in the correct order. Anyone knows why this is happening?
I have a tab with, among other stuff, following content:
<meta http-equiv="refresh" content="0; URL='whatsoever'" />
I also have a plugin running that registers every tab using this technique:
chrome.tabs.onUpdated.addListener(doStuff);
The doStuff-function sends a message to the tab:
function doStuff(tabId, changeInfo, tab){
chrome.tabs.sendMessage(tabId, {'message': 'content'}, function(response){
doOtherStuff(response);
});
}
I have a script registered (not seen here), and in that script, this happens:
function receiveMessage(request, sender, sendResponse){
sendResponse({'content': 'responseData'});
}
chrome.extension.onMessage.addListener(receiveMessage);
My problem is that due to the instantaneous refresh, the response times out, and following error appears in the console:
Could not send response: The chrome.extension.onMessage listener must
return true if you want to send a response after the listener returns
(message was sent by extension XYZ).
Why is that, and how can I circumvent this issue? Thanks in advance.
It says, the function receiveMessage should exit with return true;. Only then the message will continue to be send. On false, it is aborted the moment the function returns. So add return true
see:
This function becomes invalid when the event listener returns, unless
you return true from the event listener to indicate you wish to send a
response asynchronously (this will keep the message channel open to
the other end until sendResponse is called).
from the doc:
https://developer.chrome.com/extensions/runtime#event-onMessage
function receiveMessage(request, sender, sendResponse){
sendResponse({'content': 'responseData'});
return true;
}
chrome.extension.onMessage.addListener(receiveMessage);