TCP Socket with Javascript

Hi all,
I’m new to fuse and I’m looking to create a TCP connection within Javascript.

I saw Uno.Net.Sockets but not sure it can be called with Javascript !

There is sample somewhere about how to use Uno.Net.Sockets ?

Thank you for your help.

We have not exposed this api to JavaScript yet. You can still use this from Uno and expose it your self to JS.

Here my code

using Fuse;
using Fuse.Scripting;
using Fuse.Reactive;
using Uno.UX;
using Uno.Net.Sockets;
using Uno.Text;
using Uno.Threading;

public class MySocket : NativeModule
    static readonly MySocket _instance;
    NativeEvent _nativeEvent;
    NativeEvent _nativeEventConnected;
    NativeEvent _nativeEventError;
    Socket socket;

    public MySocket()
        // Make sure we're only initializing the module once
        if(_instance != null) return;
        _instance = this;
        Resource.SetGlobalKey(_instance, "MySocket");
        AddMember(new NativeFunction("connect", (NativeCallback)Connect));
        AddMember(new NativeFunction("send", (NativeCallback)Send));
        AddMember(new NativeFunction("read", (NativeCallback)Read));
        _nativeEvent = new NativeEvent("onMessageReceived");
        _nativeEventConnected = new NativeEvent("onConnected");
        _nativeEventError = new NativeEvent("onError");


    string Send(Context c, object[] args)
        var message = args[0] as string;
        var connected = socket.Connected;
        return "";

    string Connect(Context c, object[] args)
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);       
        var address = args[0] as string;
        debug_log address;
        int port = int.Parse(args[1] as string);
        debug_log port;
        debug_log socket;
        try {
          socket.Connect(address, port);
        } catch(SocketException e) {
        return "";

    string Read(Context c, object[] args)
        var read = new byte[] {};
        debug_log("Read data : " + Ascii.GetString(read));
        return "";


The Connect method is working fine, the Send method too … but my Read method return nothing (always blank)

Here the view :

        <StatusBarBackground Dock="Top" />
        <ScrollView ClipToBounds="true">
                <Text FontSize="30" Alignment="Center">Hello, world!</Text>
                <Button Text="Send DETECT" Clicked="{onClick}"/>
                <Button Text="Read Buffer" Clicked="{onClickRead}"/>
          var Socket = require('MySocket');
          var Observable = require('FuseJS/Observable');
          var buttonText = Observable('Button');
          Socket.connect("", "8888");
          Socket.onError = function(message) {
          Socket.onConnected = function(message) {
          Socket.onMessageReceived = function(message) {
            if(message == "SENT") {
          function onClick() {

          function onClickRead() {

          module.exports = {
              buttonText: buttonText,
              onClick: onClick,
              onClickRead: onClickRead,

The server is a nodeJS TCP server which works fine (tested with a ruby client)

Any thought about my .uno file ? How to run a loop to catch any new data in the Socket ? I’m a newbie on C#


Our socket API is based on the C# socket API. My suggestion is to have a look at the C# documentation and examples for sockets

Well, I can’t see the BeginReceive method … I don’t see where to set the callback method for reads.

Looks like it differ a bit from the C# version …:frowning:

Ok I have found how to read data … Now I need to find how to run the read inside a Thread … Do you have any Thread code sample within UNO ?

Threading in Uno is very similar to C# 2.0 even though we don’t have all the high level methods (like BeginReceive), it should be possible to read up on the general documentation.

I did write a simple project which works fine now … I hope it will help beginners

Any thoughts will be welcome :slight_smile: