michaela-damm.jpg
blocshop
September 23, 2020
0 min read

TCP networking in Cordova

TCP networking in Cordova.jpeg

Network communication is trivial in Cordova based projects. You have two main options to communicate with a server: HTTP or WebSockets.
You start having problems when you want to use something more low level. For example, if you want to implement a client to communicate with legacy services (like Telnet, IRC, etc), you need to use native TCP sockets.

Current W3C web APIs don’t expose any standardized interface and proposals like TCPSocket (https://developer.mozilla.org/en-US/docs/Web/API/TCPSocket) are still far from being implemented in web browsers.

We wanted to implement a phone and tablet application for online chess playing, that runs on three major operating systems: iOS, Android, and Windows Phone. We didn’t want to reinvent the wheel, by implementing our own chess server, so we decided to build the application on the time-honored Free Internet Chess Server (http://www.freechess.org/).

The problem is that FICS uses old fashioned TCP text based protocol, so the use of standard Web API was not an option. We could use websocket-to-socket bridge with intermediate service, but it requires hosting of service on some infrastructure, and it slightly worse reliability. Finally, we decided to implement our own Cordova plugin for TCP communication. I’ll describe it here in this blog post.

Usage

You can install plugin to your Cordova project with command line:

cordova plugin add cz.blocshop.socketsforcordova

Usage is simple, you just have to create instance of socket object:

var socket = new Socket();

Then you need to setup data consumer, error and close handlers:

socket.onData = function(data) { // invoked after new batch of data is received (typed array of bytes Uint8Array) }; socket.onError = function(errorMessage) { // invoked after error occurs during connection }; socket.onClose = function(hasError) { // invoked after connection close };

Finally you can connect to server:

socket.open( "someremoteserver.com", 1234, function() { // invoked after successful opening of socket }, function(errorMessage) { // invoked after unsuccessful opening of socket });

At this point, socket instance is in connected state and you started to receive events to handlers you setup above.
Write method of the socket accepts only Uint8Array, so you have to convert your string data. This code shows how to do it:

var dataString = "Hello world"; var data = new Uint8Array(dataString.length); for (var i = 0; i

There are 2 options of how to close the connection.

1) You can close the connection gracefully by sending FIN packet using shutdownWrite method:

socket.shutdownWrite();

After you call this method, you should wait until server sends all data to you and closes the connection. After that, you receive socket.onClose event.

2) Second options is to close socket immediately:

socket.close();

You receive socket.onClose event shortly after you call this method. Writing data to closed socket is not allowed.

More information

We created a sample Cordova project on GitHub:
https://github.com/blocshop/sockets-for-cordova-tester

The Cordova plugin itself is also available on GitHub:
https://github.com/blocshop/sockets-for-cordova

To see how this plugin works in a real-world application, you can download our Blindfold Chess Trainer game in your mobile app store (AndroidiOS) and play online. Enjoy!

Noir chess - blindfold chess trainer.png

Learn more from our insights

chatgpt4_ai_integration_blocshop-transformed.png
July 19, 2024

ChatGPT-4: An Overview, Capabilities, and Limitations

The technical aspects, usage scenarios, and limitations of ChatGPT-4, including a comparison with ChatGPT-4o.

roro665_depict_a_data_sample_thta_completely_changes_its_form_725a4f20-ea40-4dd1-a68d-5c4327c9bf24_1.png
June 20, 2024

Generative AI used for data conversions and reformatting

How to use generative AI for data conversion, addressing integrity, hallucinations, privacy, and compliance issues with effective validation and monitoring strategies.

DALL·E 2024-05-30 09.37.01 - An illustration suitable for an article about ISO 20022. The scene should feature a modern, sleek representation of the ISO 20022 logo in the center. .webp
May 28, 2024

ISO 20022 Explained: A Comprehensive Guide for Financial Institution Managers

What is ISO 20022? How does it affect companies and institutions in the fintech and banking industry and how to prepare for its adoption? All explained in this article.

DALL·E 2024-05-22 20.55.08 - A detailed and high-quality DSLR photo of a person using a laptop to shop online, showing personalized product recommendations on the screen. The back.webp
May 16, 2024

Key AI Trends in E-commerce and Overview of AI integrations for E-commerce Platforms in 2024

Transform your e-commerce platform with AI tools for personalization, analytics, chatbots, search, and fraud detection. Boost sales and improve customer experiences.

eIDAS mark.png
May 09, 2024

Digital Identity and Payment Services in the EU in 2024: Key Updates

eIDAS 2.0 and PSD3 are set to enhance how digital identities and payment services are managed across the European Union in 2024. Here’s an overview of how each framework contributes to the digital landscape of the EU, what to expect, and how to prepare.

eIDAS 2 in fintech and open banking EU market.png
May 06, 2024

What is eIDAS 2.0 and EU Digital Identity Wallet and how will it change the EU digital market

Learn how eIDAS 2.0 and the EU Digital Identity Wallet will transform digital transactions and identity management across the European Union.

best large language models for ERP systems.png
March 31, 2024

Language Models Best Suited for Integration into ERPs

Four prominent large language models stand out for their compatibility and effectiveness in ERP system processes and automation. See what they are.

PSD3 in open banking Blocshop.png
April 23, 2024

PSD2 vs. PSD3: The Evolution of Payment Services Regulation

What is PSD3 in open banking? See how PSD3 compares to PSD2 and what should banks and fintech businesses do to ensure regulatory compliance in the EU market.

roro665_hands_working_with_a_laptop_in_a_modern_office_there_is_20dca307-c993-4539-99d7-fd5ca264248c.png
April 14, 2024

Enhancing ERP Systems with AI Chatbots

Explore how AI chatbots can transform ERP systems, enhancing efficiency, decision-making, and user interaction.

eIDAS in fintech and open banking EU market.png
April 29, 2024

eIDAS: The regulation helping secure Europe's digital future

See how eIDAS enhances EU digital transactions with secure identity verification, supporting e-commerce and public services across Europe.

hybrid ERPs.png
March 21, 2024

Hybrid ERP: An Innovative Approach to Enterprise Resource Planning

Hybrid ERP is a blend of cloud and on-premise solutions. With expertise in both, Blocshop is uniquely positioned to help you with hybrid ERP development and implementation.

0-4 cover.png
October 03, 2023

IT Staffing: Individual Hiring vs. Specialized Developer Teams

Should you hire individual developers or go for a specialized, custom-built developer team?

chatgpt-35-limitations.jpg
July 17, 2023

ChatGPT-3.5: An Overview and Limitations

In this article, we'll take a closer look at the capabilities and limitations of ChatGPT-3.5, providing you with a comprehensive overview of what it can do and what its boundaries are. So, let's delve into the inner workings of this large language model.

gpt4 vs gpt3-5 and the key differnces.png
June 15, 2023

A Deep Dive into GPT-4 vs GPT-3.5 Differences and Ability to Revolutionize Software Development

There are key differences between ChatGPT-3.5 and ChatGPT-4 that software developers and companies procuring software solutions alike should be aware of. Let's see how these differences affect the output generated by these models on specific examples.

ai-development-cto-2023.jpg
May 09, 2023

AI-powered software development: What CTOs need to know in 2023

As technology continues to evolve at a rapid pace in 2023 and beyond, CTOs must stay ahead of the curve by utilizing predictive analytics, automated testing processes, and deployment solutions.

ai-web-development.jpg
May 05, 2023

How Artificial Intelligence is changing web development

AI technology is now being used in many different industries, including web development. It’s important to understand the impact that AI can have on web development as it can help companies to create more efficient and user-friendly websites.

cto-ai-software-development.jpg
May 05, 2023

How AI-powered software development is changing the role of the CTO

As AI-powered software development becomes increasingly commonplace, CTOs must prepare themselves to take on a new set of responsibilities that require more than just technical know-how.

build-mvp.jpg
April 17, 2023

How to build a minimum viable product (MVP)

The MVP is the version of a new product that allows Blocshop and your team to collect the maximum amount of validated learning about customers with the least amount of effort. The essence of your core idea is delivered as a barebones solution. The solutions is, however, sufficient for usage by early adopters. As a product, it has tangible qulities that express a look and feel.

ai-tools-developers.png
March 21, 2023

10 AI tools for developers you should know about in 2023

For developers, programmers, and data scientists, AI coding solutions can free up thinking time, allowing such professionals to focus on the fundamentals of their projects, and complete such projects much faster.

web-app-ideas.png
February 14, 2023

17 Ideas for Web Apps in 2023

Gazillions of web apps and ideas for web apps are floating around the metaverse - so creating one that properly represents a unique brand is a huge challenge. Our list provides a number of areas of simple app ideas to help businesses transform their online presence through a web app.