Connect and send message
This basic example shows how to connect to a server and send a siple chat message to another user. The steps required are the following:
- Create XmppClient and set username, password and XMPP domain properties
- call connect to create the connection, authenticate etc…
- Rquest the contact list from the server (optional)
- Send our own presence to the server
- Send a chat message from the logged in user1 to user2 on the same server which is example.com
Example
// setup XmppClient with some properties
var xmppClient = new XmppClient
{
Username = "user1",
Password = "secret",
XmppDomain = "example.com",
// setting the resolver to use the Srv resolver is optional, but recommended
HostnameResolver = new SrvNameResolver()
};
// connect so the server
await xmppClient.ConnectAsync();
// request roster (contact list). This is optional,
// but most chat clients do this on startup
var roster = await xmppClient.RequestRosterAsync();
// send our own presence to the server. This is required for most scenarios
// but there are also use cases where you may not want to publish and send you own presence
await xmppClient.SendPresenceAsync(Show.Chat, "free for Chat");
// send a chat message to user2
await xmppClient.SendChatMessageAsync("user2@exmaple.com", "This is a test");
// Close connection again
await xmppClient.DisconnectAsync();