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

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.

Top 15 micro-SaaS ideas for your startup in 2023.png
December 06, 2021

Top 15 micro-SaaS ideas for your startup in 2022

What exactly do we mean by micro SaaS? Micro Saas solutions use a web browser or mobile app interface. Micro SaaS solutions usually come about through the effort of an individual or very small team. It aims to solve precise problems. Micro SaaS projects have small budgets and overhead. Customers use Micro SaaS solutions on a monthly or yearly subscription basis. Micro SaaS projects target a small niche of the consumer market.

Software engineer hourly rates in 2021 (based on experience and location).png
November 22, 2021

Software engineer hourly rates in 2022 (based on experience and location)

Region influences salary more than any other factor. Taxes rates, cost of living, and government benefit programs affect the rates software developers charge. Software developers in the USA  and Canada earn more than software developers in other countries.

The best programming languages for app development in 2022.png
November 15, 2021

The best programming languages for app development in 2022

Software developers usually have three main ways to create an app. They can choose to code a native app, a hybrid app or a progressive web app. Developers create native apps to function on one specific platform, usually either iOS or Android. They create these apps using Swift or Objective C for iOS. For Android they use C++, Kotlin or several other languages. 

Cross-platform mobile app development: Tools & frameworks for 2022.png
November 09, 2021

Cross-platform mobile app development: Tools & frameworks for 2022

The cross-platform development project aims to create apps compatible with several operating systems. Cross-platform apps work on iOS, Android, and Windows. Cross-platform apps look and feel like apps developed specifically for the operating system.

App development cost breakdown in 2022.png
November 08, 2021

App development cost breakdown in 2022

Your business needs an app, but you aren’t sure about the cost of creating an app. Without some figures, you can’t even begin to estimate the potential budget, so let’s get you sorted with the information you need to make your app a reality.

unnamed.png
November 04, 2021

Web app development: a detailed guide

The best web apps give a responsive and engaging user experience through a browser instead of a single application. Think of web app development as a super-charged website. Web apps have many features of mobile apps coded for iOS or Android without the need to code for specific platforms. Developers create web apps using HTML, javascript, Python and CSS.

15 useful web app development tools for 2021.png
October 29, 2021

15 useful web app development tools for 2022

Web development vs app development: Choose the best for your business.png
October 19, 2021

Web development vs app development: Choose the best for your business

Outsource web development in 2021 and beyond: benefits & tips.png
October 15, 2021

Outsource web development in 2021 and beyond: benefits & tips

8 IT outsourcing trends in 2022.png
October 11, 2021

8 IT outsourcing trends in 2022

More and more firms choose to outsource their IT operations and functions. IT outsourcing grows each year. The Gartner report announced that firms spent $3.8 billion dollars on IT outsourcing in 2019. They expect that the trend will continue. Companies aiming for digital transformation need partners and tools. They need tools that they cannot build in-house with speed and accuracy. 

In-house development vs outsourcing software development.png
October 01, 2021

In-house development vs outsourcing software development

Every business starting software development must ask themselves what will serve them better, in-house or outsourcing? There is not a simple answer to the question. Making the choice to develop in-house or to outsource will have long-term consequences.

16 Software development project ideas.png
September 17, 2021

16 Software development project ideas

Every startup needs a great idea. Something unique and compelling. Startup businesses succeed when they find a customer need that they can fulfill. Startup businesses and independent software developers constantly search for just such needs.

Software development budget estimation.png
September 16, 2021

Software development budget estimation

An unlimited budget would make many teams very happy. But that approach has pitfalls. If the team works without much oversight or customer input, they may waste money. They might create features that the customers won’t use.

What are the differences between Agile and Waterfall?.png
September 07, 2021

What are the differences between Agile and Waterfall?

These days, most software development teams choose Agile methodology to organize their work. The Agile vs. Waterfall debate still rages, though. Many people question whether Agile works better than Waterfall in all circumstances. Does Agile deliver great ROI? Does Agile help teams work faster? Let’s take a close look at both Agile and Waterfall. We will examine the merits and drawbacks of each approach.