B4A Library Socket.IO Client Library

cheveguerra

Member
Licensed User
Longtime User
Is it happening on debug mode or release mode?
It is happening on release mode, if I comment the disabling and enabling of buttons everything works all right!!

If the server sends an ack object along with the message then you can use
Sub socket_notify(message As Object, ack As Object)

if the server sends only message then the signature will be
Sub socket_notify(message As Object)

Check your server code.

Thanks, that helped!!!

it works now!!

Edited code:
#Region  Project Attributes
    #ApplicationLabel: Socket.IO Simple Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #BridgeLogger : True
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim socket As SocketIOClient
    Private StatusLbl As Label
    Private SendBtn As Button
    Private AckLbl As Label
    Private MsgInput As EditText
    Private ConnectBtn As Button
    Private DisconnectBtn As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    
End Sub

Sub ConnectBtn_Click
    socket.initialize("socket")
    socket.connect("http://11.0.0.139:999/","",False)
    'socket.connectWithOptions("https://192.168.0.103:999/","", True,True, 0,1000,5000,20000) 'connect with options
    StatusLbl.Text = "Connecting"
End Sub

Sub DisconnectBtn_Click
    socket.disconnect()
    StatusLbl.Text = "Disconnecting"
End Sub

Sub socket_OnConnect
    socket.addEvent("new_message","notify")
    StatusLbl.Text = "Connected"
'    DisconnectBtn.Enabled = True
'    Log("DisconnectBtn enable true")
'    SendBtn.Enabled = True
'    Log("SendBtn enable true")
'    MsgInput.Enabled = True
'    Log("MsgInput enable true")
'    ConnectBtn.Enabled = False
'    Log("ConnectBtn enable false")
End Sub

Sub socket_OnDisconnect(Reason As Object)
    socket.removeEvent("new_message","notify")
    StatusLbl.Text = "Disconnected"
'    ConnectBtn.Enabled = True
'    Log("ConnectBtn enable true")
'    SendBtn.Enabled = False
'    Log("SendBtn enable true")
'    MsgInput.Enabled = False
'    Log("SendBtn enable false")
'    DisconnectBtn.Enabled = False
'    Log("DisconnectBtn enable false")
End Sub

Sub socket_notify(message As Object) ', ack As Object
'    socket.sendAck(ack,Array("your data"))
    Log("socket_notify="&message)
End Sub

Sub SendBtn_Click
    socket.emit("user_msg", MsgInput.Text)
End Sub

' Recibimos el callback del servidor
Sub user_msg_ack(data As Object)
    AckLbl.Text = data
    Log("user_msg_ack="&data)
End Sub
 

feriot

Member
how can send json in 'emit'?
i used this codes but no success :

B4X:
Dim m As Map
m.Initialize
m.Put("id","Client_"&id)
m.Put("msg","Hi from client")
socket.emit("user_on",m)
 

feriot

Member
it does not detect as a json obect in server side
 

Biswajit

Active Member
Licensed User
Longtime User
without any error. but does not connect.
i guess server side must use https. i'm trying to convert server codes to https edition
Check if you can browse the server ip from the device where you are running the app.

If you want to use that JSON on the server side then send JSON string instead of a map.
 

jtim3032

Member
Licensed User
Longtime User
I have been using your excellent library for some time now. However, I am just in the process of making a new app available on the Play Store and there is a new requirement that the Target SDK must be at least 30. If I set the target SDK to anything higher than 27, the client fails to connect with the following:

Error Log:
Connecting...
IOSocket_OnConnectError: io.socket.engineio.client.EngineIOException: xhr poll error
io.socket.engineio.client.EngineIOException: xhr poll error
Tring to Reconnect
Reconnecting
IOSocket_OnConnectError: io.socket.engineio.client.EngineIOException: xhr poll error
io.socket.engineio.client.EngineIOException: xhr poll error
Reconnection Error
Tring to Reconnect
Reconnecting
IOSocket_OnConnectError: io.socket.engineio.client.EngineIOException: xhr poll error
io.socket.engineio.client.EngineIOException: xhr poll error
Reconnection Error
Tring to Reconnect
Reconnecting
IOSocket_OnConnectError: io.socket.engineio.client.EngineIOException: xhr poll error
io.socket.engineio.client.EngineIOException: xhr poll error
OnReconnectFailed:
Reconnection Failed
Reconnection Error

Am I missing a part of the SDK?
 

jtim3032

Member
Licensed User
Longtime User
UPDATE:

SDK 28 and beyond by default doesn't allow non-https communication. The following has to be added to the manifest:

B4X:
CreateResourceFromFile(Macro, Core.NetworkClearText)
 

Ibrahim Saleh

Member
Licensed User
Longtime User
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (socketclient) Create ***
** Service (socketclient) Start **
Connecting...
OnError
{"message":"Invalid namespace"}
An error occurs

I'm getting such an error when connecting to my server which a tested using web socker.io Js client-side and is working well. I have tried the above response but to no fix. I really need a solution.
 

Biswajit

Active Member
Licensed User
Longtime User
You have to pass the namespace after the hostname while connecting,

B4X:
socketio.connect("<your domain / ip>:port/<namespace>", "", true/fasle)
 

feriot

Member
HTTPS connection does not work on android 10 and above with B4A v11.5. but it's ok with B4A v11.2.
app exits when connecting event fires.
 

Biswajit

Active Member
Licensed User
Longtime User
HTTPS connection does not work on android 10 and above with B4A v11.5. but it's ok with B4A v11.2.
app exits when connecting event fires.
Please check the log if its throwing any errors or not.
 

alimanam3386

Active Member
Licensed User
Longtime User
Hi, I think you need to update the lib because it can not connect to server I get this error every time, with latest version of native library on server side ( version 4.5.2 )

on server anything is ok and server run correctly , but in client side ( b4a ) I get bellow error when I try to connect.

io.socket.engineio.client.EngineIOException: xhr poll error
 

jtim3032

Member
Licensed User
Longtime User
@Biswajit I recently updated B4A from 11.00 to 11.80 (64bit). Since then, the library throws the following error after any connection attempt. Target platform is 30. Reverting to version 11.00 and the problem goes away.

Hope you can help

 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…