RTMP implementation in nodejs is possible?

Problem

i'm trying to make a implementation of RTMP in Node.js but after seeing the documentation and trying, i'm unable to, and i suspect this is not possible to make it seeing how node.js handle the streams of data.

i'm forking this code: https://github.com/timwhitlock/node-amf/tree/master/node-rtmp

according to the code and the poorly written adobe documentation, after the connection, the handshaking process starts, the clients send a stream of 1537 octets, where the first one is the rtmp version (should be always 3), the following 4 are the timestamp and the other 4 should be 0 (0000), all the rest of the data are random characters.

now, the linked library do all this, but get broken at the point of checking the 4 zero's. it uses the Net module to create a server, and listen on connection event. using the socket object provided by the event, set the encoding to "binary" (this type of encoding doesn't exist as far i see in the documentation http://nodejs.org/api/stream.html#stream_stream_setencoding_encoding ) and then try to check for the four zero's

i suspect that the socket is encoding in 'utf8' anyways, and there is where the stream get screw up, as printing the first bytes, is never the same data. changing to hex the encoding of the socket, i found matches that would indicate the four zero's i'm looking for:

V | TIME   |4 ZEROS |RANDOM DATA
03|0091c3ee|80000702|3e904115
03|0091ea60|80000702|f6e2d38a
03|00921a47|80000702|f2e21db3

03|0092eaeb|80000702|15834dbf
03|00931e0d|80000702|5f0b7891
03|00933c68|80000702|b4980c5d

even i could make it work like that, when i need to stream real data, as video, audio, etc.. , to the server (i know that i can write from the socket binary data passing a Buffer object and using this: https://github.com/substack/node-binary ) and the socket can't handle binary data, i will have corrupted data right?

i'm not an expert, but i tried to investigate this as far i can, even if this can't be solved, i'd like someone could tell me if i'm right or just i'm screwing up :)

Problem courtesy of: shadow_of__soul

Solution

well, my answer was on the same documentation, if i don't call at all setEncoding() on the socket object returned by the Net module, i can receive a plain binary Buffer, that i can parse at will, so this seems to be possible.

still, the data received are not 0's (on bytes 5,6,7 and 8 there is 128 0 7 2) so i need to know how to correctly parse binary data, but at least for now, this seems to be possible.

Regards, Shadow.

Solution courtesy of: shadow_of__soul

Discussion

This is not an encoding issue (you are not screwing it up). The closest explanation I could find is in Red5's source:

OutboundHandshake.java:96

Somehow that field is used to transmit the version of the implementation.

Discussion courtesy of: vbence

This recipe can be found in it's original form on Stack Over Flow.