Send messages to specific socket without socket.io in node.js - node.js

I'd like to send messages through a socket to specific clients but can't seem to figure out how to do it without socket.io. The reason I can't use socket.io is because I'm trying to connect to a flash client, and I've read in numerous threads here on stackoverflow that socket.io isn't meant to connect to a flash clent. I have working code that will connect a node socket server to a socket client created in flash (actionscript 3), but it broadcasts the info to everyone connected to the socket. What I thought was if there was some way to send info to specific socket ids then I'd be in business. I looked in the docs and didn't find any info on how to do this, but then again I couldn't find any documentation about specific socket ids in the docs either, yet using "socket.id" will give the id so I'm wondering if there is some undocumented way to do this.
And just FYI this is for a card game where each player connected just gets their cards sent to them. The game logic is taken care of server side and I have that working. Now I just need to get each player only the info they need. Thanks.
Darryl

I know for sure you can use Adobe® Flash® Socket in conjunction with socket.io. When you look at the available transports(browsers) you will notice that socket.io also supports Adobe® Flash® Socket. But to be honest I do not know the exact details about this, but I think you can learn them by studying socket.io's specification page.
When looking at socket.io's WIKI I also found there already is a library to use socket.io with Adobe® Flash® Socket. The library is called FlashSocket.IO which I believe can help you although I don't know any flash myself.
Adobe® Flash® Socket is standard disabled in the latest socket.io branches, but you can enable them easily by studying configuring socket.io. This page tells you how to enable all tranports which I will summarize below:
io.set('transports', [ // enable all transports (optional if you want flashsocket)
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
If you have not yet had suffice answer(I think this should help you) I think you could also file an issue at socket.io's issue page, because I find the socket.io's maintainers are super friendly and will tell you how to accomplish this without the least pain.
Finally to accomplish this using plain node you will have to keep track of all the active socket connections(sessions) yourself and only send that message to that specific socket. This is a mundane task, but I think there are more than enough tutorials available that teach you this(you could also look at socket.io how they achieve this, although this is a pretty large project/codebase).
I hope this answered your question. If not, I recommend you to ask a question in the comments for example.

Related

Questions on Nodejs socket.io chat server with TONS of rooms

I'm trying to figure out how a chat app with lots of "rooms" would be implemented with nodejs and socket.io.
I know there are plenty of tutorials around for 1 room chats that run on 1 port, but my question is how would I transform something like that to support multiple chat rooms?
Would it require a different port per room? If so how would I get the server to sniff out open ports for this?.. or is there a better approach?
The target clients are native mobile sdks like iOS and android, so how would I get the server to open a room for a GET request?
Please excuse my noobness, I'm really trying to learn.
I have to point you to ActionHeroJS, namely the built-in chat server. This is accomplished with a single websocket port on the server, where each connection and each room is stored inside Redis. When a message is broadcast to a room, only those connections in that room get the message.
I might encourage you to try out the framework as an alternative to starting from scratch, but definitely look at the concepts as a way to manage a chat system at scale, beyond single-room-tutorials.

Web socket & Flash socket clients connect to one Node.js

I've got problem connecting Flash client to Node.js server.
Short story:
For a first time I'm building a Node.js server that should be used by both web client (WebSocket) as well as a Flash client (Socket). The web client, of course, works like a charm, but I can't get over the Flash one. I get SECURITY_ERROR. After a day of research I think it's because of the policy file not being loaded. Ideas (primus on top of engine.io) ?
Long story:
I'm using Primus as I thought I'll need it because I have both web sockets and flash sockets to handle. Not sure if this is accurate? :)
I'm using Engine.io as a 'transformer/transporter' - the main framework that the layer uses. I won't discuss the standard web client (using Chrome and primus-client), as it's easy to setup.
I'm using simple and standard Sockets in AS3:
_socket = new Socket();
_socket.addEventListener(Event.CONNECT, onSocketConnect);
//...
_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
_socket.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
_socket.connect('localhost', '1337);
When building it within Flash IDE, it goes to the onSocketConnect function, but if I try to write anything to the socked - I get disconnected. If I run this from the web browser, I get into the onSecurityError method.
I must say that I don't get any traces in the node console!
primus.on('connection', function connection(spark) {
console.log('new connection'); // never gets logged!
As I know, security error is thrown when there is error with the policy file, so I started searching for a solution for that.
I've read a lot of things online, and most common solution was simple usage of socket.io and so called FlashSocket.IO. I tried implementing it, but it's so old, that some of the code is a kind of missing and I finally got some errors from the hurlant library - I couldn't get it working.
I also saw some node package called policy, which runs separate server to server the policy file.
I tried adding a transport array with flashsocket in it - no change. I also can't understand why all of the samples are using transports - I've searched and both index.js and primus.js are using transport (why there are two separate files, Jesus?!)
I could try using only engine.io without primus, but I don't know if this would be of any help. All the posts and samples I've found are pretty old - please help me with any up to date solution or at least some explanation what needs to be done - seems like a whole new universe to me :)
Thanks in advance!
Edit:
Thanks to the The_asMan, I figured out it has something to do with the handshake. I've tried this simple example (despite the fact it's so old) - it worked perfectly for the Flash client! Of course I cannot connect web sockets to it, as the handshake is not proper - it has some kind of protocol for it.
So I guess I just have to understand how to get the <policy-file-request/> in node - I'll be able to return the policy file. But I don't know how to get it - I don't receive any kind of data nor connect handler...
You have a cross domain policy issue.
I answered it all here.
AS3 - Flash/AIR Socket Communication writeUTFBytes only works once
just an idea:
On some operating systems, flush() is called automatically between execution frames, but on other operating systems, such as Windows, the data is never sent unless you call flush() explicitly. To ensure your application behaves reliably across all operating systems, it is a good practice to call the flush() method after writing each message (or related group of data) to the socket.

Is possible integrate Nodejs with Cakephp?

I want to monitor in real time the data that users enter in comments table.
I have an Apache server running, and suppose that has a node server on port 1337.
How would I do that every time someone save new data, eg return me the total number of table rows in comment and show it in a view?
Maybe way is to make the $this->Comment->save($this->request->data); using a different port using Httpsockect?
Yes, it is possible.
You have multiple ways of solving this, let me give you my ideas
You can simply use long-polling and don't use Node.js at all. It's a suitable solution if there won't be too much traffic there, otherwise you will have a bad time.
You can use websockets and don't use Node.js at all. Here you have a basic guide about websockets and PHP. Although, I am almost sure you won't be able to create "rooms", that is, sending notifications for specific comments.
You can also use Ratchet. This is a more sophisticated library to handle websockets and it supports rooms.
Lastly, if you want to full dive in with Node.js and CakePHP, I would suggest start by watching this talk given on Cakefest 2012 which exactly describe your scenario.
After you have watched that, you might want to learn a little about Socket.io. This is a more complex solution, but it's what I have used when integrating CakePHP and Node.js to create real time applications.
The strategy here is to have the users join a room when they visit /article/view/123, let's say the room name is the articleID, then socket.io will be listening for events happening in this room.
You will have a Cakephp method that handles the save. Then, when user submits the form you don't call directly the Cake action, you have socket.io to dispatch an event, then in your event you pass the data to the server (Node.js) and nodejs will call your cakephp function that saves the data. When Nodejs receives confirmation from CakePHP then you broadcast an event (using socket.io), this event will let know all users connected to that room that a comment has been made.
You have basically the choice between Websockets and long polling.
Websockets (with Ratchet and Autobahn.js)
Long Polling Using Comet
Decide which technology you want to use and start implementing your use case. Consider that Websockets are more or less new. Depending on your requirements you might not be able to use Websockets because you might have to support crappy browsers. See this page.

How to model Push Notifications on server

Brief Description:
Well, since many days I've been looking for an answer to this question but there seems to be answers for 'How to create a Push Notification Server' and like questions. I am using node.js and it's quite easy to 'create' a push notification server using sock.js (I've heard socket.io isn't good as compared to sock.js). No problem till here. But what I want is how to model such a server.
Details:
OK, so, let's say I've an application where there's a chat service (just an example this is, actual thing is big as you might have guessed). A person sends a message in a room and all the people in the room get notified. But what I want is a 'stateful' chat - that is, I want to store the messages in a data store. Here's where the trouble comes. Storing the message in the database and later telling everyone that "Hey, there's a message for you". This seems easy when we need the real-time activity for just one part of the app. What to do when the whole app is based on real-time communication? Besides this, I also want to have a RESTful api.
My solution (with which I am not really happy)
What I thought of doing was this: (on the server side of course)
Data Store
||
Data Layer (which talks to data store)
||
------------------
| |
Real-Time Server Restful server
And here, the Real-time server listens to interesting events that the data-layer publishes. Whenever something interesting happens, the server notifies the client. But which client? - This is the problem with my method
Hope you can be of help. :)
UPDATE:
I think I forgot to emphasize an important part of my question. How to implement a pub-sub system? (NOTE: I don't want the actual code, I'll manage that myself; just how to go about doing it is where I need some help). The problem is that I get quite boggled when writing the code - what to do how (my confusion is quite apparent from this question itself). Could please provide some references to read or some advice as to how to begin with this thing?
I am not sure if I understood you correctly; but I will summarize how I read it:
We have a real-time chat server that uses socket connections to publish new messages to all connected clients.
We have a database where we want to keep chat logs.
We have also a restful interface to access the realtime server to get current chats in a lazier manner.
And you want to architect your system this way:
In the above diagram, the components I circled with purple curve wants to be updated like all other clients. Am I right? I don't know what you meant with "Data Layer" but I thought it is a daemon that will be writing to database and also interfacing database for other components.
In this architecture, everything is okay in the direction you meant. I mean DataStore is connected by servers to access data, maybe to query client credentials for authentication, maybe to read user preferences etc.
For your other expectation from these components, I mean to allow these components to be updated like connected clients, why don't you allow them to be clients, too?
Your realtime server is a server for clients; but it is also a client for data layer, or database server, if we prefer a more common naming. So we already know that there is nothing that stops a server from being a client. Then, why can't our database system and restful system also be clients? Connect them to realtime server the same way you connect browsers and other clients. Let them enjoy being one of the people. :)
I hope I did not understand everything completely wrong and this makes sense for the question.

Scalable Node.js 1x1 chat platform

I am to build a chat platform over Node.js, that in core, must be able to to provide chat screens 1-1, much like Olark provides
The chat update rate is not priority, but scalability and browser compatibility are.
My question is: Which back-end strategy and which way to transmit, would be best?
EDIT:
Thanks, #Brandon_R. It is just that I am not sure if websocket is the way to go here, I am between it and AJAX.
I want my server to be able to host multiple calls, and websocket do keep a open connection for each client; isn't it limiting?
Socket.io falls back on ajax polling/other transports if websockets are not available and is probably the way to go. You can also disable websockets/other transports if you prefer not to use them.
socket.io 0.8 also has support for "rooms" which will namespace and multiplex your sockets.

Resources