As a disclaimer, I'm new to nodejs and express framework in general so please bear with me while I'm still trying to learn.
I'm using alertifyjs library to show notifications for various alerts to a user. Now, the problem is that the notifications are showing for everyone who is on the site. I get why this is happening and it makes sense. How do I go about making it so a specific alert only shows for the person that triggered it? What exactly do I use to make this happen? Cookies?
Thank you for your help and let me know if you need any more information.
Here's a code example...
//client code
socket.on('word length', function(data) {
alertify.error('Check word length!');
});
//server code
if (word.length > 50 || word.length <= 1) { // check word length:
io.sockets.emit('word length', word);
}
This is nothing to do with alertify but more to do with how socket.io works. The io.socket.emit call will broadcast a message to all connected sockets.
You want to call emit on the client socket only
io.on('connection', function (socket) {
io.socket.emit('message', 'to everyone');
socket.emit('message', 'to this client only');
});
Related
I have created a story on wit.ai using the quickstart guide.
Now I want to make a conversation with my chat bot using node-wit in node.js.
I guess I should use https://github.com/wit-ai/node-wit#runactions to run the messages, but I'm not sure how to start a conversation that never ends. I need to send a message and then get the response from the chat bot until I break the conversation.
I have looked through the wit.ai examples, but I cannot find any example of how to start a simple conversation in node.js.
I use socket.io to transmit the messages between client and server, and I have tried to solve my problem with
let sessions = {};
const sessionId = new Date().toISOString();
sessions[sessionId] = { context: {} };
io.on('connection', function (socket) {
socket.on('new message', function (message) {
client.runActions(
sessionId,
message,
sessions[sessionId].context
).then((context) => {
console.log(context);
sessions[sessionId].context = context;
}).catch((err) => {
console.error('Error: ', err.stack || err);
});
});
});
and it seems to almost work. I can chat with my bot, but it messes up the stories by sometimes answering multiple times from different stories. I guess I should probably end the stories somehow?
You should try with this link
https://github.com/wit-ai/node-wit/blob/master/examples/quickstart.js
Just clone/download the whole node-wit module from git or npm-install.
Then just run the command node quickstart.js "wit-token".
wit-token == wit-app-token
it will work .
Have you checked this Facebook Messenger integration example. The quickstart.js includes an interactive mode this is why it may be confusing.
Look at the messenger.js example on how to use runActions and send messages back to Messenger.
I was successful in doing this, although I'm still working on stories.
i want to check the user is typing a message and want to display it in my chat box, i know setting database flag and checking it by timeinterval function, but i think it should be slow method, is there any way to check it? by the users's machine ip/id .
Hi Use Websocket for this
use this link this will help you
https://www.sanwebe.com/2013/05/chat-using-websocket-php-socket
You can implement web sockets on your server, so you can pass events between your clients and server.
you can check Ratchet: http://socketo.me/ for php or Socket.io for Nodejs.
Than you can bind on "keyup" an event to notify server of typing, i suggest to throttle the events so you wont end with too many request :)
i think socket.io is what you looking for http://socket.io/get-started/chat/
I have found that using sockets is the only good solution for achieving that. https://wisembly.github.io/elephant.io/ .
PHP server side:
use ElephantIO\Client as Elephant;
$elephant = new Elephant('http://localhost:8000', 'socket.io', 1, false, true, true);
$elephant->init();
$elephant->send(
ElephantIOClient::TYPE_EVENT,
null,
null,
json_encode(array('name' => 'foo', 'args' => 'bar'))
);
$elephant->close();
echo 'tryin to send `bar` to the event `foo`';
Socket.io server side:
var io = require('socket.io').listen(8000);
io.sockets.on('connection', function (socket) {
console.log('user connected!');
socket.on('foo', function (data) {
console.log('here we are in action event and data is: ' + data);
});
});
I starting with writing a meteorjs webpage. I started with the leaderboard example. This works great. I added some new functions. I added a jquery notification script. (Noty http://ned.im/noty/ )
When I give a person five points a activate the Noty plugin by using this code
var n = noty({text: txt,timeout: 2000});
......
Template.leaderboard.events({
'click input.inc': function () {
Players.update(Session.get("selected_player"), {$inc: {score: 5}});
notje("Er heeft iemand een score gegeven");
Method.call("callHouse");
},
The notification will only displayed to my browser and not to other users. How can i use a notification to all active users.
Users -> Server -> All users
I hope you can help me with this issue
You would have to create a notifications collection (client & server):
notifications = new Meteor.Collection("notifications");
Then insert your message in there and display it using an observe handle. Make sure you add something which is user specific so that you can use a smaller subset of users. And also identify when its used and mark them as ready so they're no longer published and the user doesn't keep receiving them.
Then you could do something like this on your client side:
Meteor.startup(function() {
notifications.find().observe({
added: function(doc) {
//Message can be contained in doc.txt
//Not sure if this bit is right, but basically trigger the notification:
n = noty({text: doc.txt,timeout: 2000});
}
});
});
Hopefully this gives you a bit of a rough idea on how to do it.
The question is in the title.
I've been roaming the web for what seems like an eternity to find out how to get the client's IP after a connection.
No amount of ws.request, ws._socket, ws.adress() or ws.everyfreakingthingthereisoutthere gave me any result.The server's IP is 192.168.0.23 , the client's is 192.168.0.15 .
The connection works well (I can send, receive, connect and close connections just fine).
in it.
The server's IP is 192.168.0.23 , the client's is 192.168.0.15 .
edit: Also tried with in localhost and with another external machine on a different router (with external IP adress)
wss.on('connection', function(ws) {
console.log(ws.what?);
ws.on('message', function(message) {
console.log('received : %s', message);
});
ws.on('close', function(){
});
ws.send('HELLO');
});
I'd be very thankful for any help I can get!
EDIT : I effectively continued looking for a solution without any result.
No documentation is available online, not sure why, but getting the IP adress of the client should be an easy enough thing to do.
I'm really lost here, I've put many hours into this problem and can't seem to fix it. Everybody online seem to have different solutions working, and other not working, I'm not sure if it depends on the version, but on the latest version none of them seem to work.
Again, I'd be very grateful for any tip that would allow me to take a step forward with this issue, I'm trying my best.
EDIT : Here is a log corresponding on console.log(ws) on connection to help with the search.
You may be using the wrong object for your .on() handler:
var wss = require('http').createServer(handler).listen(80);
var io = require('socket.io').listen(wss);
wss.on('connection', function(ws) { ... // OOPS, http.server.on
io.on('connection', function(ws) { ... // CORRECT, socket.io.on
I'm guessing this is the case because your console.log(ws) object looks like an instance of node's builtin net.Socket class, not of socket.io's socket class.
Try this:
wss.sockets.on("connection", function (ws) { var address = ws.handshake.address; console.log("Ip from " + address.address + ":" + address.port); }
Simple question, maybe simple answer: how do I know on the server that a certain client has disconnected? Basic use case: the serve would need to know if a player has dropped the connection.
In the publish function, you can watch socket close event as follows.
this.session.socket.on "close", -> # do your thing
Meteor.publish("yourPublishFunction", function()
{
var id = this._session.userId;
this._session.socket.on("close", Meteor.bindEnvironment(function()
{
console.log(id); // called once the user disconnects
}, function(e){console.log(e)}));
return YourCollection.find({});
});
I've created a pretty comprehensive package to keep track of all logged-in sessions from every user, as well as their IP addresses and activity:
https://github.com/mizzao/meteor-user-status
To watch for disconnects, you can just do the following, which catches both logouts and browser closes:
UserStatus.on "sessionLogout", (userId, sessionId) ->
console.log(userId + " with session " + sessionId + " logged out")
You can also check out the code and do something similar for yourself.
Maybe (in the server code)
Meteor.default_server.sessions.length
or
Meteor.default_server.stream_server.open_sockets.length
you can do one thing make a event on server and call it from browser with ajax which call after some small time interval settimeinterval using with session values into header and if server din`t get request from user it means he dropped connection