B4i Library Socket.IO Client Library

Biswajit

Active Member
Licensed User
Here is the Socket.IO Client library wrap for this Github Project.
Click here for the B4A wrapper

iSocketIOClient
Author:
@Biswajit
Version: 1.30
Dependencies: iRandomAccessFile
  • Events:
    • Ack (data As String)
    • CustomUserEvents (data As List, ack As Object)
    • OnConnect (data As List)
    • OnConnecting (data As List)
    • OnDisconnect (data As List)
    • OnError (data As List)
    • OnReconnectAttempt (data As List)
    • OnReconnecting (data As List)
  • Functions:
    • AddListener (forEvent As String, callbackSub As String) As String
      Add custom event listener. It will return an UUID. Use that UUID to remove this particular listener.
      <b>Example:</b> <code>socket.AddListener("serverevent","appevent")</code>
    • Connect (host As String, params As Map, secure As Boolean)
      Connect SocketIO to the server
      <b>Example:</b> <code>socket.connect("http://172.0.0.1:999/",null,false)</code>
      Other parameters will have default value as followings:
      <b>reconnection</b> = True
      <b>reconnectionAttempts</b> = Infinite
      <b>reconnectionDelay</b> = 1
      <b>reconnectionDelayMax</b> = 10
      <b>timeout</b> = 30
    • ConnectWithOptions (host As String, params As Map, secure As Boolean, reconnection As Boolean, reconnectionAttempts As Int, reconnectionDelay As Int, reconnectionDelayMax As Int, timeout As Int)
      Connect SocketIO to the server with custom options
      <b>Example:</b> <code>socket.connect("http://172.0.0.1:999/",CreateMap("test":"data"),false, true, 5,1,10,30) </code>
    • DebugLog (enable As Boolean)
      Turn on debug log. Must be called before connecting. If you are already connected call disconnect then connect again.
    • Disconnect
      Disconnect SocketIO
      On successful disconnection it will raise 'OnDisconnect' event
    • EmitBinary (event As String, data As Object)
      Same as EmitString but used to send object. Usually used to forward an object to another device.
      <b>NOTE:</b> Internally it use the B4XSerializor to serialize the object.
      <b>Example:</b> <code>socket.EmitBinary("serverevent",anyObject)</code>
    • EmitString (event As String, data As Object)
      Sends a message to the server, requesting an ack.
      <b>NOTE:</b> It is upto the server send an ack back, just calling this method does not mean the server will acknowledge.
      <b>Example:</b> <code>socket.EmitString("serverevent","something")</code>
      <b>Getting Acknowledgment from server</b><code>
      Sub serverevent_Ack(data As String)
      Log(data)
      End Sub</code>
    • Initialize (bi As B4I, EventName As String)
      Initializes the Socket.io object
    • IsInitialized As Boolean
      Tests whether this object was initialized.
    • RemoveAllListener (forEvent As String)
      Remove all event listeners for given eventname
      <b>Example:</b> <code>socket.RemoveAllListener("serverevent")</code>
    • RemoveListener (listenerUUID As String)
      Remove a particular event listener
      <b>Example:</b> <code>socket.RemoveListener("uuid_received_from_AddListener")</code>
    • SendAck (ack As Object, data As String)
      Call to acknowledge receiving an event.

Installation:
  1. Its recommended to download the framework file directly from the safari of your mac.
  2. Copy XML file to B4I additional library folder.
  3. Copy .framework, .h and .a files to Libs folder of the local build server
Example Attached.
For the server code you can check Socket.IO Github example or you can use this following chat server example (written in javascript) [attached]:

JavaScript:
var express = require('express');
var app = express();

// *** FOR HTTPS CONNECTION *** //
// const fs = require('fs');

// var options = {
//   key: fs.readFileSync('./key.pem'),
//   cert: fs.readFileSync('./cert.pem'),
//   passphrase: "password of cert/key file"
// };

// var server = require('https').createServer(options, app);
// *** FOR HTTPS CONNECTION *** //


// *** FOR HTTP CONNECTION *** //
var server = require('http').Server(app);
// *** FOR HTTP CONNECTION *** //

var io = require('socket.io')(server);
var users = {};


server.listen(999, function(){
  console.log('listening on *:999');
});

io.sockets.on('connection', function(socket){
    console.log("User Connected");

    socket.on('user_msg', function(data,callback){
        io.emit('new_message', data);
        callback("Data Received");
    });

    socket.on('disconnect', function(data){
        console.log('User disconnected');
    });
});
To run this example server code:
  1. Download NodeJS.
  2. Install it.
  3. Download library zip file and extract.
  4. Run cmd inside server folder
  5. Run npm install
  6. It will take some time to complete.
  7. After that run node app.js
  8. It will show listening on *:999
  9. Now create you app and connect.
  10. That's it!
Update v1.01:
  1. Added ConnectParams method for sending additional GET parameters.
Update v1.30:
  1. Completely rewritten from scratch (though the methods are almost same)
  2. Support binary transfer.
    1. Any object you send from B4I will only accessible from B4I directly.
    2. For B4A, use B4XSerializer in B4A to convert the received object buffer to object.
    3. You cannot access the object from the server.
  3. Add multiple listeners to a particular event
  4. Remove all listeners for a particular event or one by one
  5. Now you can send multiple parameters from the server and receive them as a list.
Download all the files from here

Download previous version from here
 

Attachments

Last edited:
Top