# Not able to create websocket in swi prolog

**URL:** <https://swi-prolog.discourse.group/t/not-able-to-create-websocket-in-swi-prolog/1266>\
**Category:** Help!\
**Created:** [September 16, 2019, 12:06pm UTC](https://swi-prolog.discourse.group/t/not-able-to-create-websocket-in-swi-prolog/1266 "2019-09-16T12:06:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhijit16](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/abhijit16/32/567_2.png) [@Abhijit16](https://swi-prolog.discourse.group/u/Abhijit16)\
**Post date:** [September 16, 2019, 12:06pm UTC](https://swi-prolog.discourse.group/t/not-able-to-create-websocket-in-swi-prolog/1266/1 "2019-09-16T12:06:34Z")

</div>

Hi,

I am using SWI Prolog version 7.6.3. I am trying to create a websocket (server) in swipl (to better understand how to use websocket in SWI Prolog), and connect to it using opera/chrome browser (or other languages that have provisions to use websockets as clients with no success), but the browser comes back with this message:

# Internal server error

goal unexpectedly failed: user:http\_upgrade\_to\_websocket(echo,,[protocol(http),peer(ip(127,0,0,1)),pool(client(‘httpd@5000’,user:http\_dispatch,(0x60000347a700),(0x600003478500))),input((0x60000347a700)),method(get),request\_uri(/),path(/),http\_version(1-1),host(localhost),port(5000),connection(‘keep-alive’),upgrade\_insecure\_requests(‘1’),user\_agent(‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10\_14\_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36’),accept([media(text/html,,1.0,),media(application/‘xhtml+xml’,,1.0,),media(image/webp,,1.0,),media(image/apng,,1.0,),media(application/xml,,0.9,),media(\_900/\_902,,0.8,)]),accept\_encoding(‘gzip, deflate, br’),accept\_language(‘en-GB,en-US;q=0.9,en;q=0.8’)])

Following is the simple code that was written in SWI (basically runs a websocket as server on the machine, that prints some message when data is received at server end):

NOTE: the server is started using the goal “g” in the code below, executed from the prolog console.

```prolog
%%%%%%%%%%%%%%%%%%%%%%%%%%%%

:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/websocket)).

 :- http_handler(root(.), http_upgrade_to_websocket(echo, []), [spawn([])]).

% try hitting: http://localhost:5000 in browser.

g :- port(Port), start_server(Port).
s :- port(Port), stop_server(Port).

port(5000).
start_server(Port) :- http_server(http_dispatch, [port(Port)]).
stop_server(Port) :- http_stop_server(Port,[]).

% websocket callback function.
echo(WebSocket) :- 
    write(user_output, "in echo"), flush_output(user_output),
    ws_receive(WebSocket, Message, [format(string)]),
    ( Message.opcode == close -> true;
      (write("Message: "), write(user_output, Message.data),
       flush_output(user_output), echo(WebSocket))
    ).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

```

Any help would be much appreciated.

Regards,  
Abhijit.

---

<div class="post-metadata">

**Author:** ![Ian](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ian/32/1026_2.png) [@Ian](https://swi-prolog.discourse.group/u/Ian)\
**Post date:** [September 16, 2019, 12:36pm UTC](https://swi-prolog.discourse.group/t/not-able-to-create-websocket-in-swi-prolog/1266/2 "2019-09-16T12:36:29Z")

</div>

Install a Web Socket Client into chrome (from the Chrome Web Store) and use that to communicate with and you should see results.  
e.g. against your code:

 ![35](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/1X/ea1da9c26cfb23119f98c84b13fc2ad43b5e9188.png)

---

<div class="post-metadata">

**Author:** ![Abhijit16](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/abhijit16/32/567_2.png) [@Abhijit16](https://swi-prolog.discourse.group/u/Abhijit16)\
**Post date:** [September 17, 2019, 5:48am UTC](https://swi-prolog.discourse.group/t/not-able-to-create-websocket-in-swi-prolog/1266/3 "2019-09-17T05:48:57Z")

</div>

Thank you so much Ian, for the help !

Also, I noticed that using “http://” does not work, we have to use “ws://”, maybe using http creates problems during upgrading to websocket protocol, not sure though…
