question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TCP Microservice doesn't work with unicode characters in message

See original GitHub issue

I’m submitting a…


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

If TCP Microservice message handler tries to send a string that contains unicode character (e.g. é), the next error will appear: [Server] There is no matching event handler defined in the remote service.

Expected behavior

No errors should appear.

Minimal reproduction of the problem with instructions

@Controller()
export class TCPController {
  @MessagePattern('getUnicodeString')
  async getUnicodeString(payload: any) {
    return 'é';
  }
}

What is the motivation / use case for changing the behavior?

Environment


Nest version: 6.0.0

 
For Tooling issues:
- Node version: 11.10.0  
- Platform: Windows 

Others:

In addition

Trying to understand where this error comes from, I found out that the root of the problem is in json-socket package. So as we know Nest.js TCP Microservice server sends to client messages in the following form

export interface WritePacket<T = any> {
  err?: any;
  response?: T;
  isDisposed?: boolean;
}

When data transmission is done server sends the finishing message

{ isDisposed: true }

So if the service from the example above just sends one string of one character é, TCP Microservice server will send two mesages. The fist one

{
  err: null,
  response: 'é'
}

and the second one

{ isDisposed: true }

And here json-socket comes into play. Before each JSON.stringified message it puts the string length value and specific delimeter (#). For instance

'28#{"err":null,"response":"é"}'

Among other things this is made to distinguish messages when they come in one packet like this one

'28#{"err":null,"response":"é"}19#{"isDisposed":true}'

Under the hood json-socket uses the given length value and substring function to extract neccesary message. But considering the fact that json-socket calculate the string length with Buffer.byteLength function, the length for é character will be 2 istead of 1. This circumstance causes error as instead of first expected message

'{"err":null,"response":"é"}'

we’ll get

'{"err":null,"response":"é"}1'

Since this is not a valid json, json-socket on the client will return back to the server the next error

'148#{"success":false,"error":"Error: Could not parse JSON: Unexpected number in JSON at position 27\\nRequest data: {\\"err\\":null,\\"response\\":\\"é\\"}1"}'

The server won’t be able to define what type of message it is, as there’s no MessagePattern sent within it. Hence we get our error [Server] There is no matching event handler defined in the remote service.

Could you comment on this issue?

BTW there’s an opened pull request in the json-socket repository (https://github.com/sebastianseilund/node-json-socket/pull/46), that must address this issue. It was opened one year ago. Furthermore, it seems like the json-socket repo is not mainained for more than one year.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
kamilmysliwieccommented, Mar 26, 2019

Fixed in 6.0.3. Thanks @ivibe

1reaction
ivibecommented, Mar 20, 2019

@kamilmysliwiec ok, in progress…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Receiving Unicode characters over TCP - Stack Overflow
A TCP connection has no concept of character encoding. The data transferred at the TCP level is simply a byte stream.
Read more >
TCP Server - StreamSets Documentation
The TCP Server origin listens at the specified port numbers, establishes TCP sessions with clients that initiate TCP connections, and then processes the ......
Read more >
Use the TCP Transport - WSO2 API Manager Documentation ...
Splitting the message by a single character is the most efficient method. ... the input message by appending a special character to the...
Read more >
Configuring Logging - Quarkus
Log class mirrors the JBoss Logging API, except all methods are static . 2, Note that the class doesn't declare a logger field....
Read more >
Microservices Best Practices for Java - IBM Redbooks
IBM may not offer the products, services, or features discussed in this document in ... IBM Bluemix® and other Open Source Frameworks in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found