Ws: how do i include ws on the client side?

Created on 26 Dec 2014  ยท  14Comments  ยท  Source: websockets/ws

i have a massive php application that i would like to move from pusher to node.js using the ws library i've already fumbled around enough to get the server working and have pushed the code the app servers and they are running. now i need to update the client code, and i'm at a loss for how i might require the ws library in my html views of the php app? could you please instruct?

Most helpful comment

What the fuck? This clearly didn't answer my question

Sent from my Windows Phone


From: Arnout Kazemiermailto:[email protected]
Sent: โ€Ž12/โ€Ž27/โ€Ž2014 8:20 AM
To: einaros/wsmailto:[email protected]
Cc: Garrett R. Morrismailto:[email protected]
Subject: Re: [ws] how do i include ws on the client side? (#423)

Closed #423.


Reply to this email directly or view it on GitHub:
https://github.com/einaros/ws/issues/423#event-212196066

All 14 comments

You do not need to include the ws library in you html. Most modern browsers have support for WebSockets built in so there is no need for a client side library.

What the fuck? This clearly didn't answer my question

Sent from my Windows Phone


From: Arnout Kazemiermailto:[email protected]
Sent: โ€Ž12/โ€Ž27/โ€Ž2014 8:20 AM
To: einaros/wsmailto:[email protected]
Cc: Garrett R. Morrismailto:[email protected]
Subject: Re: [ws] how do i include ws on the client side? (#423)

Closed #423.


Reply to this email directly or view it on GitHub:
https://github.com/einaros/ws/issues/423#event-212196066

There is no library to include in your html with this module. Modern browsers support web sockets out of the box so what you need to do is write the javascript code which connects to your server and acts upon the messages you send back and forth between the server and browser.

As a simple test to see if you can connect to the node.js server you've set up you can include this in the html view(s) of your PHP:

    <script>
      var ws = new WebSocket('ws://www.your.server.com');
      ws.onmessage = function (event) {
        console.log(event.data);
      };
    </script>

Then any messages the node.js sends should appear in the console of the browser.

Here is documentation on the Web Socket API in browser.

I wanted to use the ws client from this library for congruency on client/server. I'm well aware of the standard web socket implementation, but the way the docs read, it appeared as though this offered client AND server libraries ala socket.io.
Sent from my Windows Phone


From: Trygve Liemailto:[email protected]
Sent: โ€Ž12/โ€Ž27/โ€Ž2014 8:55 AM
To: einaros/wsmailto:[email protected]
Cc: Garrett R. Morrismailto:[email protected]
Subject: Re: [ws] how do i include ws on the client side? (#423)

There is no library to include in your html with this module. Modern browsers support web sockets out of the box so what you need to do is write the javascript code which connects to your server and acts upon the messages you send back and forth between the server and browser.

As a simple test to see if you can connect to the node.js server you've set up you can include this in the html view(s) of your PHP:

    <script>
      var ws = new WebSocket('ws://www.your.server.com');
      ws.onmessage = function (event) {
        console.log(event.data);
      };
    </script>

Then any messages the node.js sends should appear in the console of the browser.

Here is documentation on the Web Socket API in browser.


Reply to this email directly or view it on GitHub:
https://github.com/einaros/ws/issues/423#issuecomment-68180495

This library does come with a server side client so one can do Web Socket connections between servers. Its not a browser client.

Guess that leaves it to me then.. I'll craft up a 3rd party plugin to mirror functionality

Sent from my Windows Phone


From: Trygve Liemailto:[email protected]
Sent: โ€Ž12/โ€Ž27/โ€Ž2014 9:39 AM
To: einaros/wsmailto:[email protected]
Cc: Garrett R. Morrismailto:[email protected]
Subject: Re: [ws] how do i include ws on the client side? (#423)

This library does come with a server side client so one can do Web Socket connections between servers. Its not a browser client.


Reply to this email directly or view it on GitHub:
https://github.com/einaros/ws/issues/423#issuecomment-68181702

Why then does the example code at https://einaros.github.io/ws/ require the library on the client side?
Any issues using the built-in browser websocket implementations?

@matanster I think when they are using a require on the client-side they are referring to using the library in a non-browser client, like another node server. Server client to server server type of thing.

Not only the readme shows the clientside usage, but also the api docs shows methods that are not implemented by the default websockets. For example https://github.com/websockets/ws/blob/master/doc/ws.md#class-websocket the ping method.

And yes, you will get a runtime error if u try to require this on the clientside.

So ill have to implement the ping method. @matanster have u wrote a 3rd party plugin?

@tsm91 a plugin? no

Yea, I found the docs to be confusing. It sure makes it look like you would use node on the browser client...

Why not provide a standard use case scenario (browser + server) as opposed to having multiple node servers communicating?

Quick question about this, would it technically be possible to use this library or at least most of it's functionality on the browser ?

I'm having trouble with the native websocket API because it's functionality is quite limited. For example, i didn't not find the way to proxy requests with the native websocket API ( new WebSocket('/proxy/') fails because all urls are required to be ws:// or wss:// ). It seems like the ws libraries just from quick testing and from looking at the API, but unfortunately, it doesn't seem to be working on the browser.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

anonaka picture anonaka  ยท  5Comments

Globik picture Globik  ยท  3Comments

nodesocket picture nodesocket  ยท  4Comments

cmnstmntmn picture cmnstmntmn  ยท  4Comments

HanHtoonAung picture HanHtoonAung  ยท  3Comments