how would I go about creating a simple socket.IO chat? - javascript

So I've been looking at socket.IO and I've stumbled upon some problems. I'm trying to create a WebSocket based chat but It seems that I can't find how I would go about creating a chat where the client is hosted on a different server than the socket.io server. I have a PHP based client that needs to connect to the server but I basically want to know how I can connect to a external socket.IO websocket server.

I would follow this really great tutorial to start with.
At this line of code:
033 socket = io.connect("http://localhost:3000");
Replace with your server address.

Related

Socket server on client side js?

I have node.js server, and I need to create dynamically updated web page with updating data. So, I thought that sockets are way to go.
But there's one problem. I need to send data from the server to client(to browser).
From my research, it is not really possible to create socket server with the client side JS. It is easy to do it the other way, but to send data only from server to client?
What would be best and easiest way to do that?
You create a webSocket or socket.io connection from your client to your server and your server is the webSocket or socket.io server. Once that connection is established, you can then freely send data either way across the connection, from client to server or from server to client.
This type of architecture is ideal for sending data from your server to a web page to dynamically update the web page as new data arrives.
webSocket is the base transport. socket.io is a layer on top of webSocket that adds a bunch of useful features such as auto-reconnect and structured messages. You can use either from a browser. webSocket support is built-in to the browser. If you want to use the additional features of socket.io, then you include the socket.io client library in your web page.
Here's a listing of some of the additional features socket.io offers over a plain webSocket: Moving from socket.io to raw websockets?.
I am not sure I have fully understood your question.
But, if I got it correctly, in order to have a "socket connection" you need to have two sides - a server and a client.
Use socket.io lib with a lightweight node.js server.
You can take a look at their docs + examples - will be very straight-forward.
If you still having trouble, write.

Host Socket.IO Offline

Im making a private network Chat.
I need to have my server and clients not connected to the internet. but the socket io grabs the javascript from the internet when it loads the page client side.
Whats the best way to fix this? can i push the socket io javascript to the client from the server?
AKA go:
http://SERVER/socket.io-1.2.0.js
instead of:
https://cdn.socket.io/socket.io-1.2.0.js
socket.io will automatically expose the client library through the HTTP/Express server it is attached to, as /socket.io/socket.io.js (so the full URL in your case would be http://SERVER/socket.io/socket.io.js)

Cordova - How to make an online app/game with a Node.js server

I've been studying node.js for a while and now want to transition to connecting to my node.js server with my cordova app.
Wherever I look, I can't seem to find any real answers or tutorials.
If I go to myIP:8080 with my phone, it connects to the server, but how would I do it with a cordova app? Everything I try that's even remotely close I end up with a bunch of errors. Guide me please. I'm confused :/
P.S I initially tried making a game using a send/receive XMLHttpRequest() with PHP/MySQL, code worked but extremely laggy.. had to find out the hard way that it is not the way to go
You can do that using Socket.io with Nodejs to get real time data from all clients.
You app will connect to your server app which will be hosted somewhere. If you are running server code on localhost (on your computer or desktop) you need to expose this using some service like ngrok. Your app will send player coordinates to server which will broadcast those coordinates to all clients/players and same for all players. If still you need any help, let me know.
In you client app, in html page load this script, not exactly but similar, it will create a socket connection (duplex connection) between your app and server app. Then you will send your coordinates to server app using socket.emit().
<script>
var socket = io('server ip');
$('#button').click(function(){
socket.emit('coordinates', {x: 1, y: 2});
});
// server can send you different events/data
// 'moved' can be any event, its name can be 'abc'
socket.on('moved', function(data){
// process this data
});
</script>
Here are some resources.
http://socket.io/
https://github.com/techpines/express.io

Socket.io - Socket connection between clients

Im looking form something to make socket connection between client, without pass through the server. Is there any node package to do this?. Server only send the other client socket, and the clients recieve and send data to each other..
Or, how can we make the implamantation. We need to make a server in client side, but we dont have node installed in the client..
The closest thing that currently exists in some form is not currently part of socket.io. It is called WebRTC and is implemented in Chrome currently. It allows the browser to connect with other browsers.
http://www.webrtc.org
The question is what kind of application is your client? If you are talking about ordinary webbrowsers you need to think about writing pluings (tough case).
If you are talking about clients you have more control then you should look at technique Puching the hole exploited by Skype or P2P apps.
http://it.slashdot.org/story/06/12/15/191205/how-skype-punches-holes-in-firewalls
In general the server is used for TCP sockets orchestration the acutall comunication go directly.

Node server and WS server running side by side?

I'm really new to Node.js and have been wondering something. I'm trying to build a simple chat room application as a way of teaching myself how this all works.
The way I see it working is that I have a Node http server which serves the pages of my site (homepage, login, chat screen etc) and then I have a WS server which accepts short messages to relay to all connected users.
The thing is I can't work out if this is the correct way to approach this. Is it possible to authenticate users on the Node server and know that they have been authenticated on the WS server? Can they share some kind of session?
Any help is appeciated!
You don't mention what you're using to implement a WebSocket server, so I'll assume you're using socket.io. (If you're not, you should.)
You can use the socket.io handshake process to access the HTTP session state.
If you're using Express (once again, highly recommended), it's easy to tie the two together.

Categories

Resources