I am new to XMPP. I have created a xmpp chat application using javascript. As of now, i am able to send and receive messages from or to the users using the following code.
jQuery.xmpp.connect({
url: url,
jid: sessionUserId,
password: password
onConnect: function ()
console.log("Xmpp Connected");
jQuery.xmpp.setPresence(null);
},
onPresence: function (presence) {
},
onMessage: function (message) {
}
});
Now I need to get last 20 conversations between two users.I had referred history in this link, and i have no idea how to use it. How can we do that. Please help me. Thanks in advance!
Related
I am currently connecting to an external XMPP server (not mine). Since I couldn't find any XMPP PHP client which suits my needs, I have developed my own simple client (XMPP PHP). The client opens a socket connection to the server and that is where the XML exchange happens, and that part works well.
The main reason for developing this package was to integrate it within a corporate app. I have done so and can successfully send messages back and forth.
Even though my end goal is to have an open websocket connection, currently the program works by polling the server in 2s interval (I am using a simple 1on1 communication, no chat rooms or similar):
receiveMessages: function () {
this.poll = setInterval(function () {
Ext.Ajax.request({
url : 'index.php',
method : 'post',
params : {
method: 'receiveMessages',
},
scope : this,
callback: function (options, success, response) {
...
}
});
}, this.pollTimer);
}
And on the PHP side:
public function receiveMessages()
{
$messages = $this->client->getMessages();
if ($messages) {
foreach ($messages as $message) {
$message = $this->xml2array($message);
$conversation = $this->conversationExists($message['#attributes']['from']);
if ($conversation == null) {
$preparedConversation = array(
...
);
$conversation = $this->_save($preparedConversation ...);
}
$message = array(
...
);
$response = $this->_save($message ...);
return array(
'success' => true,
'response' => $response,
);
}
}
}
Upon success, this method updates frontend with the received message, as well as saves the message to the DB. DB is organized in a way that one User can have many Conversations and one Conversation can have many Messages.
What I fail to understand though is how should everything be structured in order to function like some of the real chat clients (Facebook or other messengers), because this way I can't get the "concurrency" I want. If I log in with my account on 2 different places, each of them will poll the server every 2s, and it is basically a race condition, first one to get the message will display it.
If I think about it, one way to do it properly would be to implement websockets to server, and have frontend wait for DB changes, however I think this may create much read overhead on DB. Can someone give me some advice on how to do this?
I'm trying to make a chat system where anyone can go into the chat and send a message. The messages are stored in a MySQL database and my code looks like this at the moment ...
<script>
$('input[type=text]').on('keydown', function(e) {
if(e.which == 13) {
e.preventDefault();
$.ajax({
url: "php/chat.class.php",
data: { steamid: "<?php echo $steamprofile['steamid']?>", message: document.getElementById("chatMessage").value },
type: "GET",
context: document.body
}).done(function() {
alert("Message sent");
// This is when the chat should update for everyone
}).error(function() {
document.getElementById('chat-box').innerHTML += '<span class="text-muted"><i>Could not send chat message at this time.</i></span>';
});
}
});
Basically, it inserts a new row into the MySQL table once you press enter.
As it is a chat for everyone, I need the chat to update for every user when anyone sends a message.
I know how to display all the messages and who sent them, but I need it to update whenever someone sends a message, for everyone.
I've done research but I can't find anything useful unfortunately and now I'm clueless.
I've thought of updating the chat every x seconds, but I want to make the chat as smooth and fast as possible and wondering what the best solution would be.
Have a good day
So that is a read operation, and hence what you are trying is a write operation. Either you can do a continuous lookup to the server with a interval/timeout/ or initiate a rest call when last call was finished (success/error/timeout whatever). but better approach for this will be initiating a WebSocket Client and create and set up WebSocket Server in your backend and design it properly, so if it get any message from any client it will send that to all other client or something optimizer/ or something in schedule
Consider using a realtime database like Firebase https://firebase.google.com/docs/database/web/start and on your client side you can listen to messages being added using something like:
commentsRef.on('child_added', function(data) {
addCommentElement(postElement, data.key, data.val().text, data.val().author);
});
With firebase you can create a serverless chat application.
I'm writing a meteor app and working on my user registration template.
Currently I have the following code, imported on the client:
Template.register.events({
'submit form': function(event){
event.preventDefault();
let username = $('[id=input-username').val();
let email = $('[id=input-email]').val();
let password = $('[id=input-password]').val();
Accounts.createUser({
username: username,
email: email,
password: password
}, function(error){
if(error){
Bert.alert( "That username or email is either taken or invalid. Try again.", 'danger', 'growl-top-right' );
// console.log(error.reason);
}
else {
FlowRouter.go('mainLayout');
}
});
}
});
My question is, is it ok to have the Accounts.createUser code on the client or do I need to call this from a meteor method imported on the server? In my head I'm thinking a user can register as many times as they like with different emails / usernames therefore what's the harm in having the code on the client vs making a call to the server.
Thoughts welcome.
CreateUser is designed to be used from the client. It handles the encryption of the password before it is sent to the server.
You can do validations at client side to save time but ideally you should write the code in meteor method on server side and call it on client side via Meteor.call(). In your case I can simply add users using chrome console and can loop it to million times to add random stuff in your db. Csrf attacks are mostly welcome this way. You should also specify collections.allow() and collections.deny() when you are defining a new Mongo.Collection(). Also you should remove autopublish and insecure package from meteor project.
I'm trying to delete one user from _User table of parse.com but I get an error.
I'm sure that the syntax of the request is fine, I'm getting this error:
code: 206
error: "Parse::UserCannotBeAlteredWithoutSessionError"
I think I shouldn't to do log-in to delete users, because I'm doing it on API REST.
$scope.delete = function (id) {
$http({
method: "DELETE",
url: url_users + id,
headers: {'X-Parse-Application-Id': appId,
'X-Parse-REST-API-Key': restId}
}).success(function (data) {
debugger;
swal("Deleted!", "All user data has been deleted", "success");
}).error(function (data) {
debugger;
swal("Error!", "An unexpected error ocurred, try again!", "error");
});
}
You're trying to remove a user from a different user session to remove. This does not work from the REST API, even if you use the X-Parse-REST-API-Key in the headers. The solution is to use Clode Code Function to delete or update a user from another session.
For more information see the documentation Parse Cloud Code Guide
The following response is the Code Clode Function to delete a user:
Parse User Destroy - Javascript in Cloud Code
I haven't tried doing this, but I have these ideas:
Make sure your ACL allows this to the public
Is it not acceptable to call a cloud code function? This would feel more intuitive to me than using the API in this case. Can you explain why you must use the REST API?
If that doesn't work, look at this answer: https://www.parse.com/questions/delete-a-user-rest-api
Basically you might have to pass the Master Key header, which is less than ideal to expose to the public. Is it not accep
I have implemented OPENFIRE with XMPP + BOSH on my Web based client interface.
When i am sending the message ,i check whether connection is live or not.If it is not live i create new connection and then send.
Sometimes it happens that client send a message and it is not get delivered to server(not opposite client).
So for that i need a strong thing that should inform client that this message didn't reach to server,please send again.
try { // sleep(2000);
Gab.connection.send(message); >
**var request = $msg({
to: jid,
"type": "get",
id: mid
}).c('get', {
'xmlns': Strophe.NS.DISCO_INFO,
'id': mid
});**
console.log(request); >
} catch (err) {
alert("Internet Disconnected ! Click ok to Reconnect SEND");
window.top.location.reload(1);
}
On above code i call message send().After that i call function as mentioned in "XMPP:xep-0184" doc.
but no response i received.
Thank you in advance.
Not sure what a "strong thing" is, but when someone is disconnected in Strophe, you should get errors from the script and it will update the Strophe.Status connection state. Strophe sends a post every 60 secs (unless otherwise specified). In your code it appears you're trying to check support.
You should look for "Strophe.Status.CONNECTED" or "Strophe.Status.ATTACHED" prior to sending if that's your concern. You can also write a handler to manage the errors.
In XEP 0184, you must include:
<request xmlns='urn:xmpp:receipts'/>
So your msg should look like (note, you must include an id per XEP 0184):
$msg({
to: tojid,
from: fromjid,
type: "chat",
id: "sometrackingid"}).c('body').t("bodytxt").up().c("request", {
xmlns: "urn:xmpp:receipts"});
You will then need to modify you message handler or create a separate handler to manage the receipts.