Skip to content

API Mock Tutorial: Creating Websockets

Matthew Gallina edited this page Jan 10, 2019 · 5 revisions

This section covers how to create a websocket based mock.

This works much like the Feeding Responses Remotely mock type, in that clients connect to the mock websocket endpoint and then receive data pushed from the mock server from a 'queue' of available responses which are fed in from an external source.

Again like Feeding Responses Remotely the mock therefore consists of 2 parts:

  • The mocked websocket itself (i.e GET /ws), which the consumer will call and listen to for responses.
  • The queue of responses, which can be accessed from within the UI.

Creating the Mock

  • Open http://localhost:8000/ in your browser

  • Under the HTTP tab click the New HTTP Endpoint button

  • From the New HTTP Endpoint page:

    • Enter /ws under the Path field
    • Select GET under the Method field
    • Select WebSocket Proxied as the mock type
    • Leave Idle Timeout as 0
    • Leave Send Session Id on connect for now
  • Click the Save button

  • The new mock /ws, should now be present under the HTTP tab in the dashboard

  • To deploy the mock to the mock server, click the play button (or stop and then start if the server is already running)


Calling the Mock

  • From the dashboard click in the top right hand corner and select Open WS Client from the drop down menu
  • Within the WebSocket Client window enter /ws into the Path field and click the Connect button

The message Connection established should appear in the Remote Response feed after a second or so.

  • From your browser, open another tab and go to http://localhost:8000/ to open a second instance of the sMockin dashboard
  • Under the HTTP tab, locate the /ws mock in the list and click the View button.
  • On the next page, click on Active Client Connections panel, which will expand and show an active connection to the mock
  • From here click on the Message button to open the Push WS Message window
  • Enter a value such as { "msg" : "hello" } into Message Body field
  • Lastly click the Send button

Return to the original tab in your browser wherre you should now see the message { "msg" : "hello" } appear in the Remote Response feed.