I am trying to have chat in my application using Phoenix channels. I have a web client and and an Android client. Right now it is working correctly on the web. I am having an issue with the Android side.
It is able to receive messages pushed to the channel, but it wont send any out. When I try to push a message I get the following exceptions thrown:
java.net.SocketTimeoutException: timeout exception
java.lang.IllegalStateException: Another message writer is active. Did you call close()?
My chat channel
defmodule GoodApi2.ChatChannel do
use Phoenix.Channel
intercept(["chat_send"])
def join("chat:"<> _room_code, _message, socket) do
{:ok, socket}
end
def handle_in("chat_send", message, socket) do
broadcast! socket, "chat_send", message
{:noreply, socket}
end
def handle_out("chat_send", payload, socket) do
push socket, "new_message", payload
{:noreply, socket}
end
end
On the Android app creating the channel
try{
socket = new Socket("ws:"+ApiUtils.BASE.toString()+"socket/websocket");
socket.connect();
channel = socket.chan("chat:"+chatName, null);
channel.join()
.receive("ok", new IMessageCallback() {
#Override
public void onMessage(Envelope envelope) {
System.out.println("IGNORE");
}
});
}
catch (IOException e) {
System.out.println("error connecting to chat");
e.printStackTrace();
}
On the Android app pushing to the channel
public void sendMessage(final String message){
ObjectNode node = new ObjectNode(JsonNodeFactory.instance)
.put("sender", email)
.put("sender_name", userName)
.put("content", message);
try{
channel.push("chat_send", node);
}
catch (Exception e){
Log.e("message failed to send", message);
}
}
Have implemented gcm ccs for chat module and i am able to send and receive messages. Below is the main connection module,
config = XMPPTCPConnectionConfiguration.builder()
.setServiceName("gcm-pesu.googleapis.com")
.setPort(GCM_PORT)
.setHost(GCM_SERVER)
.setCompressionEnabled(false)
.setConnectTimeout(30000)
.setSecurityMode(SecurityMode.ifpossible)
.setSendPresence(false)
.setSocketFactory(SSLSocketFactory.getDefault())
.build();
connection = new XMPPTCPConnection(config);
connection.connect();
Roster roster = Roster.getInstanceFor(connection);
roster.setRosterLoadedAtLogin(false);
connection.addConnectionListener(new LoggingConnectionListener());
// Handle incoming packets
connection.addAsyncStanzaListener(new MyStanzaListener(), new MyStanzaFilter());
// Log all outgoing packets
connection.addPacketInterceptor(new MyStanzaInterceptor(), new MyStanzaFilter());
connection.login(mProjectId + "#gcm.googleapis.com", mApiKey);
logger.info("logged in: " + mProjectId);
PingManager pm = PingManager.getInstanceFor(connection);
pm.setPingInterval(300);
pm.pingMyServer();
pm.registerPingFailedListener(new PingFailedListener() {
#Override
public void pingFailed() {
connection.disconnect();
logger.error("GCM CCS, Ping failed !!");
}
});
The problem i am running into is not receiving any message from GCM, sent by client device after a while. Though, the heartbeat looks normal and i do get pong from GCM even in that case. Is it something to do with SSL ?
Have handled connection draining case as follows,
String controlType = (String) jsonObject.get("control_type");
volatile boolean connectionDraining = false;
if ("CONNECTION_DRAINING".equals(controlType)) {
connectionDraining = true;
try {
connection.disconnect();
connect();
connectionDraining = false;
} catch (Exception e) {
logger.error("Error establishing new connection after draining ", e);
}
}
Implemented queue of channels when one of it is draining.
private Deque<Channel> channels;
protected void handleControlMessage(Map<String, Object> jsonObject) {
logger.info("Control message : " + jsonObject);
String controlType = (String) jsonObject.get("control_type");
if ("CONNECTION_DRAINING".equals(controlType)) {
connectionDraining = true;
}
}
Create new channel while sending message
public void sendDownstreamMessage(String jsonRequest) {
Channel channel = channels.peekFirst();
try {
if (channel.connectionDraining) {
synchronized (channels) {
channel = channels.peekFirst();
if (channel.connectionDraining) {
channels.addFirst(connect());
channel = channels.peekFirst();
}
}
}
channel.send(jsonRequest);
} catch (Exception e) {
logger.error("Message not sent. Error in connecting :", e);
}
}
GCM will take care of closing the other. This resolved the issue.
I believe you're facing a common case using gcm css that is not very visible in the documentation.
If you look in the doc, Control Messages you'll read:
Periodically, CCS needs to close down a connection to perform load balancing. Before it closes the connection, CCS sends a CONNECTION_DRAINING message to indicate that the connection is being drained and will be closed soon. "Draining" refers to shutting off the flow of messages coming into a connection, but allowing whatever is already in the pipeline to continue. When you receive a CONNECTION_DRAINING message, you should immediately begin sending messages to another CCS connection, opening a new connection if necessary. You should, however, keep the original connection open and continue receiving messages that may come over the connection (and ACKing them)—CCS handles initiating a connection close when it is ready.
Background:
I have been able to implement one to one chatting through XMPP in android using asmack library. I am able to send presence to the server as well. I am using OpenFire server for my Chat based application.
Problem:
I am using connection.addPacketListener(new PacketListener() to receive message and IQ packets, for message packets I have classified it like this
PacketFilter Chatfilter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
Log.i("XMPPClient", "Got text [" + message.getBody() + "] from [" + fromName + "]");
messages.add(fromName + ":");
m1=message.getBody();
messages.add(message.getBody());
// Add the incoming message to the list view
/* mHandler.post(new Runnable() {
public void run() {
setListAdapter();
recieve.setText(m1);
}
});*/
}
}
}, Chatfilter);
And it is working all fine, but problem arises when I use something similar to receive IQ packets
Here is the code which I am using to receive IQ PACKETS
PacketFilter Iqfilter = new IQTypeFilter(IQ.Type.RESULT);
connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
IQ iq = (IQ) packet;
String fromName = StringUtils.parseBareAddress(iq.getFrom());
Log.i("XMPPClient", "Got text [" + iq.toString() + "] from [" + fromName + "]");
m1=iq.getFrom();
mHandler.post(new Runnable() {
public void run() {
setListAdapter();
recieve.setText(m1);
}
});
}
}, Iqfilter);
I am sending a simple disco#items query and it does not respond, even it does not enter into the function, I have tried to debug it as well, I have also tried to send simple PING command but it does not respond to it either. what am I missing here?
Secondly I am facing problems in sending IQ packets to the server as well or to some other client as well. I read somewhere that I should do it like this. but it does not work.
final IQ iq = new IQ() {
public String getChildElementXML() {
return "<query xmlns='http://jabber.org/protocol/disco#info'/>"; // here is your query
//this returns "<iq type='get' from='User#YourServer/Resource' id='info1'> <query xmlns='http://jabber.org/protocol/disco#info'/></iq>";
}};
// set the type
iq.setType(IQ.Type.GET);
// send the request
connection.sendPacket(iq);
The confusing thing is when I read the documentation of XMPP and asmack for android it was written that to send an IQ you need to have receiver's address as well. but in this code we are not setting up any receiver.
There is much less information available on internet for XMPP asmack and Android.
I think the problem is that your providers aren't registered. Please take a look at this. On Android you have to register the providers manually before you form an XMPP connection. Copy the class below into your project
import org.jivesoftware.smack.provider.PrivacyProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smackx.GroupChatInvitation;
import org.jivesoftware.smackx.PrivateDataManager;
import org.jivesoftware.smackx.packet.ChatStateExtension;
import org.jivesoftware.smackx.packet.LastActivity;
import org.jivesoftware.smackx.packet.OfflineMessageInfo;
import org.jivesoftware.smackx.packet.OfflineMessageRequest;
import org.jivesoftware.smackx.packet.SharedGroupsInfo;
import org.jivesoftware.smackx.provider.AdHocCommandDataProvider;
import org.jivesoftware.smackx.provider.BytestreamsProvider;
import org.jivesoftware.smackx.provider.DataFormProvider;
import org.jivesoftware.smackx.provider.DelayInformationProvider;
import org.jivesoftware.smackx.provider.DiscoverInfoProvider;
import org.jivesoftware.smackx.provider.DiscoverItemsProvider;
import org.jivesoftware.smackx.provider.MUCAdminProvider;
import org.jivesoftware.smackx.provider.MUCOwnerProvider;
import org.jivesoftware.smackx.provider.MUCUserProvider;
import org.jivesoftware.smackx.provider.MessageEventProvider;
import org.jivesoftware.smackx.provider.MultipleAddressesProvider;
import org.jivesoftware.smackx.provider.RosterExchangeProvider;
import org.jivesoftware.smackx.provider.StreamInitiationProvider;
import org.jivesoftware.smackx.provider.VCardProvider;
import org.jivesoftware.smackx.provider.XHTMLExtensionProvider;
import org.jivesoftware.smackx.search.UserSearch;
import android.util.Log;
public class ServiceProviders {
public static void Register_Providers(ProviderManager pm) {
// Private Data Storage
pm.addIQProvider("query", "jabber:iq:private",
new PrivateDataManager.PrivateDataIQProvider());
// Time
try {
pm.addIQProvider("query", "jabber:iq:time",
Class.forName("org.jivesoftware.smackx.packet.Time"));
} catch (ClassNotFoundException e) {
Log.w("TestClient",
"Can't load class for org.jivesoftware.smackx.packet.Time");
}
// Roster Exchange
pm.addExtensionProvider("x", "jabber:x:roster",
new RosterExchangeProvider());
// Message Events
pm.addExtensionProvider("x", "jabber:x:event",
new MessageEventProvider());
// Chat State
pm.addExtensionProvider("active",
"http://jabber.org/protocol/chatstates",
new ChatStateExtension.Provider());
pm.addExtensionProvider("composing",
"http://jabber.org/protocol/chatstates",
new ChatStateExtension.Provider());
pm.addExtensionProvider("paused",
"http://jabber.org/protocol/chatstates",
new ChatStateExtension.Provider());
pm.addExtensionProvider("inactive",
"http://jabber.org/protocol/chatstates",
new ChatStateExtension.Provider());
pm.addExtensionProvider("gone",
"http://jabber.org/protocol/chatstates",
new ChatStateExtension.Provider());
// XHTML
pm.addExtensionProvider("html", "http://jabber.org/protocol/xhtml-im",
new XHTMLExtensionProvider());
// Group Chat Invitations
pm.addExtensionProvider("x", "jabber:x:conference",
new GroupChatInvitation.Provider());
// Service Discovery # Items
pm.addIQProvider("query", "http://jabber.org/protocol/disco#items",
new DiscoverItemsProvider());
// Service Discovery # Info
pm.addIQProvider("query", "http://jabber.org/protocol/disco#info",
new DiscoverInfoProvider());
// Data Forms
pm.addExtensionProvider("x", "jabber:x:data", new DataFormProvider());
// MUC User
pm.addExtensionProvider("x", "http://jabber.org/protocol/muc#user",
new MUCUserProvider());
// MUC Admin
pm.addIQProvider("query", "http://jabber.org/protocol/muc#admin",
new MUCAdminProvider());
// MUC Owner
pm.addIQProvider("query", "http://jabber.org/protocol/muc#owner",
new MUCOwnerProvider());
// Delayed Delivery
pm.addExtensionProvider("x", "jabber:x:delay",
new DelayInformationProvider());
// Version
try {
pm.addIQProvider("query", "jabber:iq:version",
Class.forName("org.jivesoftware.smackx.packet.Version"));
} catch (ClassNotFoundException e) {
// Not sure what's happening here.
}
// VCard
pm.addIQProvider("vCard", "vcard-temp", new VCardProvider());
// Offline Message Requests
pm.addIQProvider("offline", "http://jabber.org/protocol/offline",
new OfflineMessageRequest.Provider());
// Offline Message Indicator
pm.addExtensionProvider("offline",
"http://jabber.org/protocol/offline",
new OfflineMessageInfo.Provider());
// Last Activity
pm.addIQProvider("query", "jabber:iq:last", new LastActivity.Provider());
// User Search
pm.addIQProvider("query", "jabber:iq:search", new UserSearch.Provider());
// SharedGroupsInfo
pm.addIQProvider("sharedgroup",
"http://www.jivesoftware.org/protocol/sharedgroup",
new SharedGroupsInfo.Provider());
// JEP-33: Extended Stanza Addressing
pm.addExtensionProvider("addresses",
"http://jabber.org/protocol/address",
new MultipleAddressesProvider());
// FileTransfer
pm.addIQProvider("si", "http://jabber.org/protocol/si",
new StreamInitiationProvider());
pm.addIQProvider("query", "http://jabber.org/protocol/bytestreams",
new BytestreamsProvider());
// Privacy
pm.addIQProvider("query", "jabber:iq:privacy", new PrivacyProvider());
pm.addIQProvider("command", "http://jabber.org/protocol/commands",
new AdHocCommandDataProvider());
pm.addExtensionProvider("malformed-action",
"http://jabber.org/protocol/commands",
new AdHocCommandDataProvider.MalformedActionError());
pm.addExtensionProvider("bad-locale",
"http://jabber.org/protocol/commands",
new AdHocCommandDataProvider.BadLocaleError());
pm.addExtensionProvider("bad-payload",
"http://jabber.org/protocol/commands",
new AdHocCommandDataProvider.BadPayloadError());
pm.addExtensionProvider("bad-sessionid",
"http://jabber.org/protocol/commands",
new AdHocCommandDataProvider.BadSessionIDError());
pm.addExtensionProvider("session-expired",
"http://jabber.org/protocol/commands",
new AdHocCommandDataProvider.SessionExpiredError());
pm.addIQProvider("query", "http://jabber.org/protocol/disco#info",
new DiscoverInfoProvider());
pm.addExtensionProvider("x", "jabber:x:data", new DataFormProvider());
// pm.addExtensionProvider("status ","", new XMLPlayerList());
}
}
Call this method like this
ServiceProviders.Register_Providers(ProviderManager.getInstance());
So try calling this method at the start of your app or before forming a connection.
Hope this helps
When sending a self-define message, for example,
<iq id='123' type='get' from='client#xmpp/B' to='client2#xmpp/s2'><req var='read'><attr var='temprature'/></req></iq>
You need to define your own IQ and send this IQ:
public class MyIQ extends IQ {
#Override
public String getChildElementXML() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<req var='read'>< attr var='temprature'/></req>");
return stringBuilder.toString();
}
}
MyIQ packet = new MyIQ();
packet.setType(IQ.Type.GET);
packet.setFrom("client#xmpp/B");
packet.setTo("client2#xmpp/s2");
xmppConnection.sendPacket(packet);
For sent out, you actually just need to construct the xml string and return in getChildElementXML(), no other setup needed.
As mentioned in this blog post: https://billynyh.github.io/blog/2014/09/28/android-xmpp-dev/
For receiving IQ response, first you need to create IQProvider and add
to ProviderManager, matching the xmlns in the query object. The
IQProvider is used to convert the response packet to your customized
IQ object. Then you need to extends PacketListener, and when you
create the connection object, add the listener to it, with a
PacketTypeFilter to match your IQ class. With both provider and
listener setup correctly, you should be able to receive the response
of the IQ.
Good morning, I have the following code:
int numero = 22492;
String PhnNoStr = String.valueOf (numero);
String transaction = WriteText(tipoTransacao);
String SmsStr = "ECART" + "" + transaction + idToken;
System.out.println ("Message:" + smsStr);
MessageConnection msgCon = null;
msgCon = (MessageConnection) Connector.open ("sms :/ /" + + phnNoStr ": 500");
TextMessage TxtMsg = (TextMessage) msgCon.newMessage (MessageConnection.TEXT_MESSAGE);
txtMsg.setPayloadText (smsStr);
msgCon.send (TxtMsg);
so I send that message by default it returns me a message. I can send and receive this message, however I need to intercept when I receive this message, does anyone know how I can do this?
Thank you
You can use PushRegistry to have your midlet launched when a SMS is received and midlet is not running.
PushRegistry.registerConnection("sms://:500", "your_package.Your_MIDlet", "*");
To handle incoming SMS, you need to open connection and listen for incoming message, eg:
class SMSHandler implements MessageListener, Runnable {
public void start() {
...
connection = (MessageConnection) Connector.open("sms://:500", Connector.READ);
connection.setMessageListener(this);
}
public void notifyIncomingMessage(MessageConnection messageConnection) {
(new Thread(this)).start();
}
public void run() {
final Message message = connection.receive();
...
}
(The reason for processing the message in another thread is that blocking I/O should not be done in system callback - at least WTK emulator will print warning, and on some phones midlet will just freeze).
I have some clients, they communicate with one server and I need that server forward the message to another second server. Then, receive the message from the second server and send to the client.
With this method, I achieve connecting to the second server but it doesn't receive the message and throws me the following exception:
EXCEPTION: java.nio.channels.NotYetConnectedException. java.nio.channels.NotYetConnectedException
public void messageReceived(ChannelHandlerContext ctx, final MessageEvent e) throws IOException, Exception {
response = "hola" + "\r\n";
Main.creaLog("Mensaje recibido del conc: " + e.getMessage().toString());
Main.creaLog("Mensaje enviado al servidor : " + response);
ClientBootstrap bootstrap1 = new ClientBootstrap(
new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
// Configure the pipeline factory.
//bootstrap1.setPipelineFactory(new CLIENTE.ClientePipelineFactory());
bootstrap1.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() {
return Channels.pipeline(new ClienteHandler());
}
});
final ChannelFuture future = bootstrap1.connect(new InetSocketAddress("172.16.10.14", 12355));
Channel channel = future.getChannel();
if (channel.isWritable()) {
ChannelFuture lastWriteFuture = channel.write(e.getMessage().toString() + "\r\n");
}
close = true;
// We do not need to write a ChannelBuffer here.
// We know the encoder inserted at TelnetPipelineFactory will do the conversion.
ChannelFuture future = e.getChannel().write(response + "\r\n");
//CIERRA LA CONEXION
if (close) {
future.addListener(ChannelFutureListener.CLOSE);
}
}
I'm very thanksful if anybody can help me.
Have a look at Netty Proxy example
Right now, you are basically attempting to connect to the remote server on every message that you receive. This probably isn't what you want. You might want to connect to the remote server only once (i.e. outbound channel in Netty proxy example) and forward a new incoming message to that specific channel.