ddd
ddd

Reputation: 3

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?

Upvotes: 0

Views: 985

Answers (2)

jfriend00
jfriend00

Reputation: 708026

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?.

Upvotes: 1

matan yemini
matan yemini

Reputation: 161

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.

Upvotes: 1

Related Questions